反射新实例构造函数 java 参数数量错误
reflection newinstance constructor java wrong number of arguments
我正在 Java 中学习 Reflection,并试图制作一个 Constructor 的示例。但是有一个问题:"wrong number of arguments"。
通过 google 和 Whosebug 搜索,我找不到与我目前面临的问题相同的问题。
谁能帮我理解这个问题,非常感谢。
这是我的代码:
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);
}
}
这就是错误
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)
再次感谢您帮助我理解问题。 :)
您正在不带参数地调用两个构造函数中的第一个。但是你如何确定数组中的第一个构造函数是没有参数的构造函数,而不是期望字符串的构造函数?
你不能,因为 the javadoc 说:
The elements in the array returned are not sorted and are not in any particular order.
如果你想引用 class 的无参数构造函数,你应该调用
cl.getDeclaredConstructor();
如果您想要引用以字符串作为参数的构造函数,您应该调用
cl.getDeclaredConstructor(String.class);
我正在 Java 中学习 Reflection,并试图制作一个 Constructor 的示例。但是有一个问题:"wrong number of arguments"。 通过 google 和 Whosebug 搜索,我找不到与我目前面临的问题相同的问题。 谁能帮我理解这个问题,非常感谢。 这是我的代码:
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);
}
}
这就是错误
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)
再次感谢您帮助我理解问题。 :)
您正在不带参数地调用两个构造函数中的第一个。但是你如何确定数组中的第一个构造函数是没有参数的构造函数,而不是期望字符串的构造函数?
你不能,因为 the javadoc 说:
The elements in the array returned are not sorted and are not in any particular order.
如果你想引用 class 的无参数构造函数,你应该调用
cl.getDeclaredConstructor();
如果您想要引用以字符串作为参数的构造函数,您应该调用
cl.getDeclaredConstructor(String.class);