import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
class Person {
private String name;
private int age;
Person(String name,int age){
this.name=name;
this.age=age;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
public class Test1 {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
// 获取字节码对象
Class c = Person.class;
Constructor con = c.getConstructor(String.class, int.class);
Object obj = con.newInstance("林青霞",27);
System.out.println(obj);
}
}
/*Exception in thread "main" java.lang.NoSuchMethodException: util.Person.<init>(java.lang.String, int)
at java.lang.Class.getConstructor0(Class.java:2849)
at java.lang.Class.getConstructor(Class.java:1718)
at util.Test1.main(Test1.java:28)
*/
|
|