为什么不可能通过Java中的反射构建私人对象

发布于 2025-02-07 17:46:03 字数 1175 浏览 2 评论 0原文

我清楚地将构造函数设置为True可以访问,但是为什么仍然是一个错误!您能告诉我如何修复它

public class Person {
    // fields...

    private Person(String name, int age, String address, int num) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.num = num;
        System.out.println("private full-constructor be created");
    }

}

//错误发生吗?为什么?

public static void operatePrivateConstructorFullParameter(Class<? extends Person> clzz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<? extends Person> constructor = clzz.getConstructor(String.class, int.class, String.class, int.class);
    constructor.setAccessible(true);

    Person person = constructor.newInstance("Miao", 18, "MOON", 88);
}


public static void main(String[] args) throws Exception {
    Class<? extends Person> clzz = Person.class;
    operatePrivateConstructorFullParameter(clzz);
}

I clearly set the constructor's setAccessible to true, but why is it still an error! can you tell me how to fix it

public class Person {
    // fields...

    private Person(String name, int age, String address, int num) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.num = num;
        System.out.println("private full-constructor be created");
    }

}

// error happend ?! why?

public static void operatePrivateConstructorFullParameter(Class<? extends Person> clzz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<? extends Person> constructor = clzz.getConstructor(String.class, int.class, String.class, int.class);
    constructor.setAccessible(true);

    Person person = constructor.newInstance("Miao", 18, "MOON", 88);
}


public static void main(String[] args) throws Exception {
    Class<? extends Person> clzz = Person.class;
    operatePrivateConstructorFullParameter(clzz);
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我早已燃尽 2025-02-14 17:46:03

此错误与可访问性无关,而是要查找构造函数。

如其记录的合同中指定的,方法 class.getConstructor() 只会找到 public 构造函数。

如果您想找到此私有构造函数,则需要使用 getDeclaredConstructor() 使用正确的参数类型,

一旦找到构造函数,您仍然需要使其可访问。但是,请注意,在模块化应用程序中,封装可以阻止您以这种方式使用反射。

This error isn't about accessibility, it's about finding the constructor.

As specified in its documented contract, the method Class.getConstructor() will only find public constructors.

If you want to find this private constructor, you need to use getDeclaredConstructor() with the correct parameter types

Once you find the constructor, you will still need to make it accessible. Be aware, however, that in a modular application, encapsulation can prevent you from using reflection this way.

作业与我同在 2025-02-14 17:46:03

尝试一下

Constructor<Person> constructor= (Constructor<Person>) Person.class.getDeclaredConstructors()[0];

Try this

Constructor<Person> constructor= (Constructor<Person>) Person.class.getDeclaredConstructors()[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文