1、首先建立一个对象这里就是customer。下面有它的类。java
2、构建默认的无参数的构造对象ide
3、构建有参数的构造对象测试
代码以下:this
package reflect;.net
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;对象
/**
* 复制对象。
* @author pkx
*
*/
public class CopyClass {
public Object copyObject(Object o,Object[] parameters,Class<?>... parameterTypes) throws Exception{
Class cls = o.getClass(); //
Constructor con = null;
Object copyObject = null;
if(parameters==null || parameterTypes==null){get
//无参数数的构造方法 参数为空的new Class[]{}
con = cls.getDeclaredConstructor(new Class[]{});string
//无参数的构造方法生成对象默认是空的参数 new Object[]{}
copyObject = con.newInstance(new Object[]{});
}else{hash
//带参数的构造方法,参数按顺序
con=cls.getDeclaredConstructor(parameterTypes);io
//建立带构造方法参数的对象
copyObject=con.newInstance(parameters);
}
//得到对象的字段
Field[] fields =cls.getDeclaredFields();
for(Field field:fields){
String fieldName = field.getName();
String methodName=fieldName.replace(fieldName.substring(0,1),fieldName.substring(0,1).toUpperCase());
String setMethodName="set"+methodName;
String getMethodName="get"+methodName;
//设置set方法
Method setMethod = cls.getMethod(setMethodName,new Class[]{field.getType()});
//设置get方法
Method getMethod = cls.getMethod(getMethodName,new Class[]{});
//get 方法获取值
Object value = getMethod.invoke(copyObject, new Object[]{});
System.out.println("value::"+value);
//set方法设置值
setMethod.invoke(copyObject, new Object[]{value});
}
return copyObject;
}
public Object copyDefaultObject(Object o) throws Exception{
return copyObject(o,null,null);
}
public static void main(String args[]) throws Exception{
Customer c = new Customer("ni hao ",21);
CopyClass copyClass = new CopyClass();
//测试带参数的构造方法
Object cc = copyClass.copyObject(c,new Object[]{"小明",11},new Class[]{String.class,Integer.class});
Customer cc2 = (Customer)cc;
System.out.println("getName-"+cc2.getName()+"-age-"+cc2.getAge());
//测试无参数的构造方法
Customer cu =new Customer();
Customer cu2=(Customer)copyClass.copyDefaultObject(cu);
cu2.setName("xiaoli");
System.out.println("name:"+cu2.getName());
}
}
package reflect;
public class Customer {
private Integer id;
private String name;
private Integer age;
public Customer(){}
public Customer(String name,Integer age){
super();
this.name=name;
this.age=age;
}
public Customer(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public Integer getAge() { return age; } /** * @param age the age to set */ public void setAge(Integer age) { this.age = age; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", getId()=" + getId() + ", getName()=" + getName() + ", getAge()=" + getAge() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; } }