简体   繁体   English

反射newinstance构造函数java错误的参数数量

[英]reflection newinstance constructor java wrong number of arguments

I am learning Reflection in Java, and trying to make an example of Constructor. 我正在学习Java中的Reflection,并尝试创建一个Constructor的例子。 But there is a problem: "wrong number of arguments". 但是有一个问题:“参数数量错误”。 Searching through google and stackoverflow, I cannot find the same problem as I am currently facing. 通过谷歌搜索和stackoverflow,我找不到与我当前面临的相同的问题。 can anyone please help me to understand the problem, thank you so much. 谁能帮助我了解问题,非常感谢。 This is my codes: 这是我的代码:

    public static void main(String[] args) {
    PrintClass f = new PrintClass();
    Class cl = f.getClass();
    Constructor<?> constructor[] = cl.getDeclaredConstructors(); // cl.getDeclaredConstructors() also won't work...

    f.field1 = 3;
    PrintClass p1 = null;
    PrintClass p2 = null;

    try {
        p1 = (PrintClass) constructor[0].newInstance(); // constructor[0].newInstance((Object[])args) also won't work...
        p2 = (PrintClass) constructor[1].newInstance("this is not PrintClass-------");

        p1.print();
        p2.print();

    } catch (InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
}

class PrintClass {
String s = "this is PrintClass...";
int field1;

public PrintClass(String s) {
    this.s = s;
}

public PrintClass() {
}

public void print() {
    System.out.println(s);
}

} }

and this is the error 这是错误

java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at reflection.MainClass.main(MainClass.java:18)

Again, thank you so much for helping me understand the problem. 再次感谢您帮助我理解问题。 :) :)

You're calling the first of the two constructors without argument. 您正在调用不带参数的两个构造函数中的第一个。 But how can you be sure that the first constructor in the array is the one without argument, and not the one expecting a String? 但是如何确定数组中的第一个构造函数是没有参数的构造函数,而不是期望String的构造函数?

You can't, because the javadoc says: 您不能,因为javadoc说:

The elements in the array returned are not sorted and are not in any particular order. 返回的数组中的元素未排序,并且没有任何特定顺序。

If you want a reference to the no-argument constructor of the class, you should call 如果要引用该类的无参数构造函数,则应调用

cl.getDeclaredConstructor();

If you want a reference to the constructor taking a STring as argument, you should call 如果要引用以STring作为参数的构造函数,则应调用

cl.getDeclaredConstructor(String.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java反射constructor.newInstance给出“错误数量的参数” - java reflection constructor.newInstance gives "wrong number of arguments" IllegalArgumentException:Java Constructor.newInstance() 中的参数数量错误 - IllegalArgumentException: wrong number of arguments in Java Constructor.newInstance() 使用反射:java.reflection.Constructor.newInstance() - Using Reflection: java.reflection.Constructor.newInstance() Java反射 - 参数数量错误; 预期0,得1 - Java Reflection - Wrong number of arguments; expected 0, got 1 使用Java Constructor.newInstance(args),为什么出现“错误数目的args”错误? - Using Java Constructor.newInstance(args) , why the “wrong number of args” error? 为什么我得到 Java.lang.IllegalArgumentException:使用反射创建带有私有构造函数的新实例时参数数量错误 - Why do I get Java.lang.IllegalArgumentException: wrong number of arguments while creating new instance with private constructor using reflection Java:如何在Java Reflection中使用非原始类型的constructor.newInstance()? - Java: How to use non-primitive types for constructor.newInstance() in Java Reflection? Java Reflection,如何使用构造函数获取newInstance是SomeObject.Class,例如:Class <Customer> 客户类别 - Java Reflection , how to get newInstance with constructor is SomeObject.Class ex: Class<Customer> customerClass Java参数数量错误 - Java wrong number of arguments Java中具有可变数量的参数的构造方法 - Constructor with variable number of arguments in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM