构造类对象
使用构造器新建对象。根据指定的参数类型找到相应的构造函数,传入相应参数调用执行,以创建一个新的对象实例。
private static void operateConstructor(String className) throws Exception{
Class clazz = Class.forName(className);
//调无参public的构造方法,太简单了,直接调newInstance();
//Object obj = clazz.newInstance();
//相当于要实现: Person p = new Person("Jack",10);
//1先获取“Person(String name, int age)”这个构造方法--Constructor对象--con
Class parameterTypes[] = new Class[2]; //组织形参
parameterTypes[0] = String.class;
//parameterTypes[1] = int.class;//※※※Person类中的构造方法为“public Person(String name, int age)”时,OK。但构造方法为“public Person(String name, Integer age)”时,WA。
//parameterTypes[1] = Integer.class; //当构造方法为“public Person(String name, int age)”时,不行,因为它会在匹配构造方法时,严格匹配第2个参数为“Int