public static void testTT() throws Exception {
Class<?> clazz = Class.forName("reflect.Person");
// 获得构造器
Constructor<?> constructor = clazz.getDeclaredConstructor(new Class[] {});
// 根据类的默认构造器来获得一个对象
Object instance = constructor.newInstance(new Object[] {});
System.out.println(instance);
Constructor<?> constructor2 = clazz
.getDeclaredConstructor(new Class[] { Integer.class, String.class, Integer.class });
Object instance2 = constructor2.newInstance(new Object[]{1,"Tom",21});
System.out.println(instance2);
}
public Person(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
Exception in thread "main" java.lang.NoSuchMethodException: reflect.Person.<init>(java.lang.Integer, java.lang.String, java.lang.Integer)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)