【问题标题】:invoke inner class method using outer class instance使用外部类实例调用内部类方法
【发布时间】:2014-12-28 02:11:07
【问题描述】:

我有一个 PersonStoryDe​​rivedTest 类,它有一个内部类“InnerClass”,其中包含函数 foo。

如果我只有一个 PersonStoryDe​​rivedTest 的实例,并且没有类名/构造函数/方法...并且我想调用 foo(我知道它是 hte 内部类中的第二个方法)。我该怎么做?

public class PersonStoryDerivedTest extends PersonStoryTest {

    private void thePersonRests(Integer hours) {
        person.rest(hours);
    }

    public void ff(String g){
        System.out.println("Alex");
    }

    public class InnerClass extends PersonStoryTest {
        public void aPerson(Integer age) {
            person = new Person(age);
        }

        public void foo(String g){
            System.out.println("David");
        }
    }
}

public class test {

    public static void main(String[] args) {

        PersonStoryDerivedTest a = new PersonStoryDerivedTest();
        Method[] g1 =  a.getClass().getDeclaredMethods();
        g1[1].invoke(a,"fff");   // print Alex (works well)

        PersonStoryDerivedTest.InnerClass ab = a.new InnerClass();
        Class<?>[] b = a.getClass().getDeclaredClasses(); 
        Method[] g =  b[0].getDeclaredMethods();
        g[1].invoke(ab,"fff"); // print David  (works well)
        g[1].invoke( b[0] ,"fff");   // (does not work... how can I create the appropriate instance     needed by only having b[0])
        }
}

thnx

【问题讨论】:

标签: java invoke inner-classes


【解决方案1】:

我认为您对反射的要求太多了,例如: Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection?

以下方法有效,但前提是您将构造函数添加到类中。当没有构造函数时,我无法让它工作:

public class Person
{
    public Person(int age)
    {
        System.out.println("person with age " + age);
    }
    public void rest(int hours)
    {
        System.out.println("person rests " + hours);
    }
}

public class PersonStoryDerivedTest extends PersonStoryTest {
    public PersonStoryDerivedTest()
    {
        System.out.println("PersonStoryDerivedTest cnstr.");
    }
    private void thePersonRests(Integer hours) {
        person.rest(hours);
    }
    public void ff(String g){
        System.out.println("Alex");
    }

    public class InnerClass extends PersonStoryTest {
        public InnerClass() { System.out.println("InnerClass cnstr."); }
        public void aPerson(Integer age) {
            person = new Person(age);
        }

        public void foo(String g){
            System.out.println("David");
        }
    }
}

public class PersonStoryTest
{
    public PersonStoryTest()
    {
        System.out.println("PersonStoryTest cnstr.");
    }
    Person person;
}

import java.lang.reflect.*;
public class test {
public static void main(String[] args) throws Exception {
    PersonStoryDerivedTest a = new PersonStoryDerivedTest();
    Method[] g1 =  a.getClass().getDeclaredMethods();
    g1[1].invoke(a,"fff");   // print Alex (works well)

    PersonStoryDerivedTest.InnerClass ab = a.new InnerClass();
    Class<?>[] b = a.getClass().getDeclaredClasses(); 
    Method[] g =  b[0].getDeclaredMethods();
    g[1].invoke(ab,"fff"); // print David  (works well)
    Constructor<?>[] cs = b[0].getConstructors();
    Object o = cs[0].newInstance(a);
    g[1].invoke( o ,"fff");   // now works too... 
    }
}

打印输出:

PersonStoryTest cnstr.
PersonStoryDerivedTest cnstr.
Alex
PersonStoryTest cnstr.
InnerClass cnstr.
David
PersonStoryTest cnstr.
InnerClass cnstr.
David

【讨论】:

【解决方案2】:

外部类实例没有对内部类实例的引用。只有内部类实例具有对外部类实例的引用。多个内部类实例可以引用同一个外部类实例。

【讨论】:

  • 但我可以通过以下方式获取内部类:“Class>[] b = a.getClass().getDeclaredClasses();”
  • 该类是静态类型结构。要调用一个方法,你需要一个类的实例,而不是它的类型。
  • true.. 但是我怎样才能只使用外部类 + 反射的实例来创建这个实例?
猜你喜欢
  • 2019-04-16
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多