package Test;
public class JavaBean {
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("Test.Person"); //利用反射创建 实例
Person p = (Person)clazz.newInstance();
/*问题就报在这里了。Exception in thread "main" java.lang.InstantiationException: Exam.Person
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at Exam.JavaBean.main(JavaBean.java:24) */
}
}
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age;
public Person(String name, int age){
this.age=age;
this.name=name;
}
}
|
|