BeanUtils.copyProperties()和JSONObject.parseObject()分别是哪种拷贝类型(浅拷贝 or 深拷贝)

一、结论

  • BeanUtils.copyProperties():浅拷贝
  • JSONObject.parseObject():深拷贝

二、证明BeanUtils.copyProperties()是浅拷贝

代码:

import org.springframework.beans.BeanUtils;

public class Test {
    public static void main(String[] args) throws Exception {
        Info info1 = new Info();
        Student student = new Student();
        student.setName("李华");
        student.setAge(18);
        info1.setCountry("中国");
        info1.setStudent(student);

        Info info2 = new Info();
        BeanUtils.copyProperties(info1, info2);
        info2.setCountry("American");
        info2.getStudent().setName("Tom");
        info2.getStudent().setAge(18);

        System.out.println(info1);
        System.out.println(info2);
    }
}

class Info {

    /** 国家信息 **/
    private String country;

    /** 学生基本信息 **/
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Info{" +
                "country='" + country + '\'' +
                ", student=" + student +
                '}';
    }
}

/**
 * 引用数据类型
 */
class Student {

    /** 姓名 **/
    private String name;

    /** 年龄 **/
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

结果:

Info{country='中国', student=Student{name='Tom', age=18}}
Info{country='American', student=Student{name='Tom', age=18}}

解释:

从结果中可以看到student都是Tom,说明这是浅拷贝

拓展:

不太懂BeanUtils.copyProperties()使用的请看BeanUtils.copyProperties(A, B)的使用

三、证明JSONObject.parseObject()是深拷贝

代码:

class Test {
    public static void main(String[] args) throws Exception {
        Info info1 = new Info();
        Student student = new Student();
        student.setName("李华");
        student.setAge(18);
        info1.setCountry("中国");
        info1.setStudent(student);

        Info info2 = JSONObject.parseObject(JSONObject.toJSONString(info1), new TypeReference<Info>(){});
        info2.setCountry("American");
        info2.getStudent().setName("Tom");
        info2.getStudent().setAge(18);

        System.out.println(info1);
        System.out.println(info2);
    }
}

class Info {

    /** 国家信息 **/
    private String country;

    /** 学生基本信息 **/
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Info{" +
                "country='" + country + '\'' +
                ", student=" + student +
                '}';
    }
}

/**
 * 引用数据类型
 */
class Student {

    /** 姓名 **/
    private String name;

    /** 年龄 **/
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

结果:

Info{country='中国', student=Student{name='李华', age=18}}
Info{country='American', student=Student{name='Tom', age=18}}

解释:

从结果中可以看到student对象的名字是不一样的,说明是深拷贝

四、总结

例子中的Info类以及属性Student类都没有实现Cloneable接口,也没有重写clone()方法,说它们是浅拷贝或者深拷贝意义不大,但是道理是差不多的,如果想搞懂什么是浅拷贝,什么是深拷贝,请观看引用拷贝、浅拷贝、深拷贝

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值