使用外部类实例调用内部类方法

时间:2014-12-27 17:09:16

标签: java invoke inner-classes

我有一个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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我认为你想从反射中得到太多,例如: 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