java 复制树_利用 java 反射复制新对象

一、首先创建一个对象这里就是customer。下面有它的类。

二、构建默认的无参数的构造对象

三、构建有参数的构造对象

代码如下:

package reflect;

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){

//无参数数的构造方法 参数为空的new Class[]{}

con = cls.getDeclaredConstructor(new Class[]{});

//无参数的构造方法生成对象默认是空的参数 new Object[]{}

copyObject = con.newInstance(new Object[]{});

}else{

//带参数的构造方法,参数按顺序

con=cls.getDeclaredConstructor(parameterTypes);

//创建带构造方法参数的对象

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值