嗨,我想使用反射获取内部类的对象,但我遇到了一些错误。
代码是: -
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)