5月17日 CMS 周四

本文通过一个具体的Java程序示例介绍了如何使用Apache Commons BeanUtils库进行对象属性复制的方法。该示例展示了如何将两个不同类的对象属性进行映射,并通过控制台输出验证了属性复制的效果。
package com.www;

public class Cola {
    private String name;
    public String press;
    public int price;
    public Cola() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Cola(String name, String press, int price) {
        super();
        this.name = name;
        this.press = press;
        this.price = price;
    }
    @Override
    public String toString() {
        return "Pepsi [name=" + name + ", press=" + press + ", price=" + price
                + "]";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPress() {
        return press;
    }
    public void setPress(String press) {
        this.press = press;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
}
package com.www;

public class Pepsi {
    private String name="百事可乐";
    public String press="百事公司";
    public int price=2;
    public Pepsi() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Pepsi(String name, String press, int price) {
        super();
        this.name = name;
        this.press = press;
        this.price = price;
    }
    @Override
    public String toString() {
        return "Pepsi [name=" + name + ", press=" + press + ", price=" + price
                + "]";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPress() {
        return press;
    }
    public void setPress(String press) {
        this.press = press;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }


}
package com.www;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class Test {
    public static void main(String[] args) throws Exception, InvocationTargetException {
        Pepsi p = new Pepsi();
        Cola c = new Cola();

        BeanUtils.copyProperties(c, p);

        System.out.println(c.getName());
        System.out.println(c.getPress());
        System.out.println(c.getPrice());
    }
}

Evolution forged the entirety of Sentient life on this planet using only one tool— the mistake.进化形成了这个星球上有感情和知觉的生命体用的唯一的工具,就是错误。
《西部世界》

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值