3

嗨,我想使用反射获取内部类的对象,但我遇到了一些错误。

代码是: -

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)
4

4 回答 4

1

用这个:

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)它说:

...如果此 Class 对象表示在非静态上下文中声明的内部类,则形式参数类型包括显式封闭实例作为第一个参数。

因此,将封闭实例作为第一个参数将创建内部类的新实例:

    TestReflection innerInstance = (TestReflection) constructor
            .newInstance(outerInstance);
于 2013-09-30T12:52:54.243 回答
1

因为TestReflection是内部类,它只能存在于外部类的实例中TestReflectionTestReflection因此,您必须在实例化内部类时提供一个实例。

用这个:

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");
        }
    }

}
于 2013-09-30T12:55:51.383 回答
0

首先,非静态内部类只能使用外部类实例来实例化,例如

Outer outer = new Outer();
Inner inner = outer.new Inner();

其次 - 据我所知,java反射api不提供实例化非静态内部类的能力。只有静态的才有可能。因此,如果您不需要内部类是动态的并被外部实例引用 - 只需为内部类添加一个“静态”修饰符,您的代码就可以工作。

于 2013-09-30T12:43:49.140 回答
0

设置TestReflectionstatic,如:

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实例又名:Class.forName("reflaction.MyReflection").newInstance();然后从该实例加载"reflaction.MyReflection$TestReflection"

于 2013-09-30T12:43:58.427 回答