简体   繁体   中英

invoke inner class method using outer class instance

I have a class PersonStoryDerivedTest which has an inner class "InnerClass" which has function foo in it.

if I have only an instance a of PersonStoryDerivedTest, and dont have the class name/constructor/methods... and I want to invoke foo (I know it is the second method in hte inner class). how can I do it?

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

An outer class instance doesn't have a reference to an inner class instance. Only an inner class instance has a reference to an outer class instance. Multiple inner class instances can have a reference to the same outer class instance.

I think you want too much from reflection, see for example: Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection?

The following works, but only if you add constructors to the classes. When there are no constructors, I could not get it working:

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... 
    }
}

prints output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM