为什么访问第二个构造函数时会发生数组越界啊?
程序代码:
package fanshe.ConstructorDemo; import java.lang.reflect.Constructor; class Person { private int age; private String name; Person() { } Person(String name,int age) { this.name=name; this.age=age; } public void setName(String name) { this.name=name; } public String getName() { return this.name; } public void setAge(int age) { this.age=age; } public int getAge() { return this.age; } public String toString() { return "姓名:"+this.name+"\r\n"+"年龄:"+this.age; } } public class InstanceDemo04 { public static void main(String[] args) { Class<?> c=null; try { c=Class.forName("fanshe.ConstructorDemo.Person"); } catch(ClassNotFoundException e) { e.printStackTrace(); } Person per=null; Constructor<?>[] cons = null ; cons=c.getConstructors(); try { per=(Person)cons[1].newInstance("java爱好者",20); //per=(Person)cons[0].newInstance(); } catch(Exception e2) { e2.printStackTrace(); } //per.setAge(20);per.setName("java爱好者"); System.out.println(per); } }