class Person{
private String name ;
private String sex ;
public Person(){
System.out.println("c");
}
public Person(String c1){
this.name =c1;
System.out.println("c1"+c1);
}
public Person(String c1,String c2){
this.name =c1;
this.sex =c2;
System.out.println("c1"+c1 +"c2"+c2);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} }
 public  static  void  main(String [] args){
try {
Class<?> clazz = Class.forName("com.leeeason.miaosha.Person") ; // 取得Class对象
Constructor<?>[] constructors = clazz.getConstructors();
System.out.print(constructors.length);
Object o = constructors[0].newInstance();
} catch (Exception e) {
e.printStackTrace();
} }

报错java.lang.IllegalArgumentException: wrong number of arguments

反射报错java.lang.IllegalArgumentException: wrong number of arguments-LMLPHP

因为构造函数取值的时候是从下往上 从0开始

  public  static  void  main(String [] args){
try {
Class<?> clazz = Class.forName("com.leeeason.miaosha.Person") ; // 取得Class对象
Constructor<?>[] constructors = clazz.getConstructors();
//System.out.print(constructors.length);
//修改后
Object o = constructors[2].newInstance();
} catch (Exception e) {
e.printStackTrace();
} }

反射报错java.lang.IllegalArgumentException: wrong number of arguments-LMLPHP

返回成功

05-11 13:52