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() + "]"; } }