内部类不能存在于父类之外。您需要先构造父类。如果不经过反思,它将看起来像:
InnerClass innerClass = new TestClass().new InnerClass();
反过来,您需要在构造内部类时传递父类。
Object testClass = Class.forName("com.example.TestClass").newInstance();for (Class<?> cls : testClass.getClass().getDeclaredClasses()) { // You would like to exclude static nested classes // since they require another approach. if (!Modifier.isStatic(cls.getModifiers())) { Object innerClass = cls .getDeclaredConstructor(new Class[] { testClass.getClass() }) .newInstance(new Object[] { testClass }); }}


