代码如下:
public static void invokeOtherClassConstructor() {
Class<?> demo = null;
try {
demo = Class.forName("reflect.Person");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Person per1=null;
        Person per2=null;
        Person per3=null;
        Person per4=null;
        Constructor<?> cons[] = demo.getConstructors();
        try {
per2=(Person)cons[0].newInstance("Rollen");
        per3=(Person)cons[1].newInstance(20);
        per4=(Person)cons[2].newInstance("Rollen",20);
        per1 = (Person)cons[3].newInstance();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
        System.out.println(per1);
        System.out.println(per2);
        System.out.println(per3);
        System.out.println(per4);
}public static void main(String[] args) {
invokeOtherClassConstructor();
}
为什么run的时候会报java.lang.IllegalArgumentException: wrong number of arguments的错误?而debug就正确了。

解决方案 »

  1.   

    per2=(Person)cons[0].newInstance("Rollen");
            per3=(Person)cons[1].newInstance(20);
            per4=(Person)cons[2].newInstance("Rollen",20);
            per1 = (Person)cons[3].newInstance();问题应该在这里吧,可能在RUN和DEBUG下,获取的构造函数顺序不相同。
    而你在DEBUG下刚好是正确的,而在RUN下则出现问题。
    DEBUG下,con[0] = Person(String name);
             con[1] = Person(Integer age);
             con[2] = Person(String name,Integer age);
             con[3] = Person();
    而RUN下,这些构造函数排列顺序不相同。
      

  2.   

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package cn.dzr.question;import java.lang.reflect.Constructor;
    import java.lang.reflect.TypeVariable;/**
     *
     * @author dzr
     */
    public class ReflectTest {
        public static void main(String[] rags) throws ClassNotFoundException
        {
            Class clazz = Class.forName("cn.dzr.domain.Person");
            Constructor[] cons =  clazz.getConstructors();
            
            int i = 1;
            for(Constructor con: cons)
            {
                System.out.print("第"+i+"个构造函数的名字 ");
                System.out.println(con.getName());
                System.out.println("第"+i+"个构造函数的参数列表 ");
    //            TypeVariable[] vars = con.getTypeParameters();
    //             System.out.println(vars);
    //             System.out.println(vars.length);
    //             System.out.println("下面打印TypeVariable");
                 Class[] cs = con.getParameterTypes();
                 System.out.println("该构造函数的参数个数为: "+cs.length);
    //            for(TypeVariable var :vars)
    //            {
    //                System.out.println(var.getName());
    //            }
    //            System.out.println("下面打印Class");
                 if(cs==null||cs.length==0)
                     System.out.println("这是一个无参构造函数!");
                 
                 else
                    for(Class c:cs)
                    {
                        System.out.print(c.getName()+ "   ");                }
                
                System.out.println();
                i++;
            }
        }    
    }
      

  3.   

    我用netbeans,在控制台输出的结果都是一样的。是与person类中的顺序(从上往下)相反。
      

  4.   

    不要这样弄,明确指出参数类型,获取对应的构造方法,clazz.getConstructor(parameterTypes),
    记得int.class,和Integer.class是不一样的,constructor(1),类型是int,
    lang3 
    有ConstructorUtils#public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes)
    可以自己去研究,同个包下有很多反射的工具类