我有带String[]
构造函数的类(没有默认构造函数):
我需要按名称动态构造一个实例:
package tests;
import java.lang.reflect.Constructor;
interface I {
}
class A implements I {
public A(String[] args) {
System.out.printf("Args = %s", args.toString());
}
}
public class DynaCon {
public static void main(String[] args) throws Exception {
@SuppressWarnings("unchecked")
Class<I> clz = (Class<I>) Class.forName("tests.A");
Constructor<I> ctr = clz.getDeclaredConstructor(args.getClass());
ctr.newInstance(args);
}
}
为什么会引发 IllegalArgumentException?