首页 新闻 会员 周边 捐助

关于Java获取内部类

0
悬赏园豆:5 [已解决问题] 解决于 2019-06-19 17:48

打比方一个类里边有多个内部类,怎样获取该类里边指定的某一个内部类
public class FactoryTest {

@Test
public void test2(){
    FactoryTest factoryTest = new FactoryTest();
    Class<? extends FactoryTest> clazz = factoryTest.getClass();
    Class<?>[] classes = clazz.getDeclaredClasses();
    System.out.println(Arrays.toString(classes)); //只能获取一个数组

结果:[class com.design.factory.FactoryTest$Triangle, classcom.design.factory.FactoryTest$Square, class com.design.factory.FactoryTest$Circle, interface com.design.factory.FactoryTest$Shape]
}

interface Shape{
    public abstract void draw();
}

class Circle implements Shape{
    public void draw() {
        System.out.println("draw Circle");
    }
}
class Square implements Shape{
    public void draw(){
        System.out.println("draw Square");
    }
}
class Triangle implements Shape{
    public void draw(){
        System.out.println("draw Triangle");
    }
}

}

迷人的二哈的主页 迷人的二哈 | 初学一级 | 园豆:199
提问于:2019-06-18 18:07
< >
分享
最佳答案
0

首先你要明白你这个获取内部类的方法叫反射,然后网上搜一下有很多,参考:
https://blog.csdn.net/ldstartnow/article/details/52782420

收获园豆:5
小光 | 小虾三级 |园豆:1766 | 2019-06-19 10:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册