您好我想使用反射获取内部类的对象,但我收到了一些错误。
代码是: -
package reflaction;
public class MyReflection {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class obj = Class.forName("reflaction.MyReflection$TestReflection");
TestReflection a = (TestReflection) obj.newInstance();
a.demo();
}
class TestReflection {
public void demo(){
System.out.println("reflection occurs");
}
}
}
,错误是: -
Exception in thread "main" java.lang.InstantiationException: reflaction.MyReflection$TestReflection
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at reflaction.MyReflection.main(MyReflection.java:10)
答案 0 :(得分:2)
使用此:
public class MyReflection {
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException {
Class outer = Class.forName("reflaction.MyReflection");
Object outerInstance = outer.newInstance();
Class<?> inner = Class
.forName("reflaction.MyReflection$TestReflection");
Constructor<?> constructor = inner.getDeclaredConstructor(outer);
TestReflection innerInstance = (TestReflection) constructor
.newInstance(outerInstance);
innerInstance.demo();
}
class TestReflection {
public void demo() {
System.out.println("reflection occurs");
}
}
查看getDeclaredConstructor(Class<?>... parameterTypes)
的{{3}}。它说:
...如果此Class对象表示在非静态上下文中声明的内部类,则形式参数类型包括显式封闭实例作为第一个参数。
因此,将封闭实例作为第一个参数将创建内部类的新实例:
TestReflection innerInstance = (TestReflection) constructor
.newInstance(outerInstance);
答案 1 :(得分:1)
因为TestReflection
是一个内部类,所以它只能存在于外部类TestReflection
的实例中。因此,在实例化内部类时,必须提供TestReflection
的实例。
使用此:
public class MyReflection {
public static void main(String[] args) throws Exception {
Class<?> testReflectionClass = Class
.forName("reflection.MyReflection$TestReflection");
MyReflection myReflection = new MyReflection();
Object newInstance = testReflectionClass.getDeclaredConstructor(
MyReflection.class).newInstance(myReflection);
TestReflection testReflection = (TestReflection) newInstance;
testReflection.demo();
}
class TestReflection {
public void demo() {
System.out.println("reflection occurs");
}
}
}
答案 2 :(得分:0)
首先,非静态内部类只能使用外部类实例进行实例化,例如
Outer outer = new Outer();
Inner inner = outer.new Inner();
其次 - 据我所知,java reflection api不提供实例化非静态内部类的能力。它只适用于静态的。因此,如果您不需要内部类是动态的并且使用外部实例引用 - 只需为内部类添加“静态”修饰符,您的代码就可以工作。
答案 3 :(得分:0)
设置TestReflection
类static
,例如:
package reflaction;
public class MyReflection {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> obj = Class.forName("reflaction.MyReflection$TestReflection");
TestReflection a = (TestReflection) obj.newInstance();
a.demo();
}
static class TestReflection {
public void demo(){
System.out.println("reflection occurs");
}
}
}
另一种方法是创建MyReflection
实例aka:Class.forName("reflaction.MyReflection").newInstance();
,然后从该实例加载"reflaction.MyReflection$TestReflection"