BeanUtils.copyProperties使用

BeanUtils.copyProperties使用,第1张

BeanUtils.copyProperties使用

拷贝说明
拷贝:对基本数据类型进行值传递,对引用数据类型进行引用传递般的拷贝,此为浅拷贝
深拷贝:对基本数据类型进行值传递,对引用数据类型,创建一个新的对象,并复制其内容,此为

BeanUtils.copyProperties
BeanUtils.copyProperties 为深度拷贝,复制对象的一切属性

pom

     
        
            org.springframework
            spring-beans
            5.2.8.RELEASE
        
      
      
     
            com.alibaba
            fastjson
            1.2.66
     

代码

Father&Life

import lombok.Data;

@Data
public class Father {
    private String face; // 长相
    private String height; // 身高
    private Life life; // 生命

}

@Data
class Life {
    private String status;
}

Son

import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;

public class Son extends Father {
    private Life life;

    public static void main(String[] args) {
        Father source = new Father();
        source.setFace("handsome");
        source.setHeight("180");
        Life lifes = new Life();
        lifes.setStatus("alive");
        source.setLife(lifes);
        Son target = new Son();
        //  深度拷贝
        BeanUtils.copyProperties(source, target);
        System.out.println(JSON.toJSONString(source));
        System.out.println(JSON.toJSONString(target));
    }
}

测试

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5672300.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存