打比方一个类里边有多个内部类,怎样获取该类里边指定的某一个内部类
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");
}
}
}
首先你要明白你这个获取内部类的方法叫反射,然后网上搜一下有很多,参考:
https://blog.csdn.net/ldstartnow/article/details/52782420