利用反射复制数据

利用反射复制数据
package reflect;

public class Student {

private String name ;
private String address;
protected String phone;
public String scord;

public Student(String name, String address, String phone, String scord) {
    super();
    this.name = name;
    this.address = address;
    this.phone = phone;
    this.scord = scord;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
public String getScord() {
    return scord;
}
public void setScord(String scord) {
    this.scord = scord;
}
@Override
public String toString() {
    return "Student [name=" + name + ", address=" + address + ", phone=" + phone + ", scord=" + scord + "]";
}

}
package reflect;

public class Person {

private String name ;
private String address;
protected String phone;
public String scord;

public Person(){

}
public Person(String name, String address, String phone, String scord) {
    super();
    this.name = name;
    this.address = address;
    this.phone = phone;
    this.scord = scord;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
public String getScord() {
    return scord;
}
public void setScord(String scord) {
    this.scord = scord;
}
@Override
public String toString() {
    return "Person [name=" + name + ", address=" + address + ", phone=" + phone + ", scord=" + scord + "]";
}

}

public class UserBeanTest {

public static void main(String[] args) throws Exception {

     Student student=new Student("student","henan","123456","123.45");
     Person person = new Person();
     test(student,person);
}

/**
 * 将源数据中的数据复制给object对象的相同类型相同弄名称的属性
 * @param bean 源数据
 * @param object 目标数据
 * @throws Exception
 */
public static void test(Object bean,Object object ) throws Exception {

    Field[] beanFields = bean.getClass().getDeclaredFields();
    Field[] objectFields = object.getClass().getDeclaredFields();
    for (Field field : beanFields) {
        field.setAccessible(true);
        Object val = field.get(bean);
        for (Field field2 : objectFields) {
            field2.setAccessible(true);
            if (field2.getName().equals(field.getName())&& field.getType().equals(field2.getType())) {
                field2.set(object, val);
            }
        }
    }
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值