想生成对象的实体,在反射动态机制中有两种方法,一个针对无变量的构造方法,一个针对带参数的构造方法,,如果想调用无参数的构造函数直接调用Class类中的newInstance(),而如果想调用有参数的构造函数,则需要调用Constructor类中newInstance()方法,首先准备一个Class[]作为Constructor的参数类型。然后调用该Class对象的getConstructor()方法获得一个专属的Constructor的对象,最后再准备一个Object[]作为Constructor对象昂的newInstance()方法的实参。
在这里需要说明的是 只有两个类拥有newInstance()方法,分别是Class类和Constructor类,Class类中的newInstance()方法是不带参数的,而Constructro类中的newInstance()方法是带参数的需要提供必要的参数。
下面提供的代码是构造Customer2类的三个构造函数
程序执行结果如下:
显然,第一行是执行的两个参数的构造函数,int类型的age初始化为0,第二行执行的是带三个参数的构造函数,最后,第三行执行的是无参数的 构造函数。 ,Long,String类型的id,name都初始化为null,而int类型的age初始化为0。
在这里需要说明的是 只有两个类拥有newInstance()方法,分别是Class类和Constructor类,Class类中的newInstance()方法是不带参数的,而Constructro类中的newInstance()方法是带参数的需要提供必要的参数。
下面提供的代码是构造Customer2类的三个构造函数
java 代码
package cn.com.reflection; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; /** * 在反射Reflection机制中,想生成一个类的实例有两种方法 * 一个是针对无参数的构造函数 ,另一个是针对有参数的构造函数 * */ public class ReflecTest3 { /** * 反射的动态性质之一: 运行期动态生成Instance * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws SecurityException * @throws IllegalArgumentException */ public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { Customer2 customer=new Customer2(); Class cls=customer.getClass(); //获得Class所代表的对象的所有类型的构造函数Constructor的数组 Constructor ctor[]=cls.getDeclaredConstructors(); for(int i=0;i //获得对应的构造函数参数列表的Class类型的数组 Class cx[]=ctor[i].getParameterTypes(); if(cx.length==0){ //无参的构造函数可以通过Class实例直接调用Class类的newInstance()方法 Object obj=cls.newInstance(); //同样也可以象以下这样构造,调用Constructor类的newInstance()方法 //Customer2 obj=(Customer2)cls.getConstructor(cx).newInstance(new Object[]{}); System.out.println(obj); }else if(cx.length==2){ //此时只能调用Constructor类的newInstance()方法,注意:利用反射调用的是私有private的构造方法 Customer2 obj=(Customer2)cls.getConstructor(cx).newInstance( new Object[]{new Long(123),"hejianjie"}); System.out.println(obj); }else if(cx.length==3){ //此时只能调用Constructor类的newInstance()方法,注意:利用反射调用的是公有public的构造方法 Customer2 obj=(Customer2)cls.getConstructor(cx).newInstance( new Object[]{new Long(133),"China-Boy",new Integer(21)}); System.out.println(obj); } } } } class Customer2{ private Long id; private String name; private int age; /** * 无参数的构造函数 * */ public Customer2(){ } /** * public修饰的有参数的构造函数,3个参数 * @param id * @param name * @param age */ public Customer2(Long id,String name,int age){ this.id=id; this.name=name; this.age=age; } /** * public修饰的构造函数,2个参数 * @param id * @param name */ public Customer2(Long id,String name){ this.id=id; this.name=name; this.age=age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return ("id=="+this.getId()+" Name=="+this.getName()+" Age:"+this.getAge()); } }
程序执行结果如下:
java 代码
id==123 Name==hejianjie Age:0 id==133 Name==China-Boy Age:21 id==null Name==null Age:0
显然,第一行是执行的两个参数的构造函数,int类型的age初始化为0,第二行执行的是带三个参数的构造函数,最后,第三行执行的是无参数的 构造函数。 ,Long,String类型的id,name都初始化为null,而int类型的age初始化为0。