package StudyOne;
import java.lang.reflect.Constructor;
class students{
private String name;
private int age;
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;
}
public String toString(){
return "我的名字叫"+this.name+"今年"+this.age+"岁";
}
}
public class BeansOne {
public static void main(String[] args) throws Exception{
Class clazz=Class.forName("StudyOne.students");
Constructor cons[]=clazz.getConstructors();
students s=(students) cons[0].newInstance("张三",30);
System.out.println(s);
}
}
执行后会抛出一个Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0的异常。