java中对象的深拷贝和浅拷贝的区别联系

一 对象拷贝

1.1 浅拷贝

1.浅拷贝:

对于基本数据复制的是值;

引用类型复制的是内存引用地址,源对象只是复制了一份引用地址给拷贝对象,拷贝对象和源对象在堆内存中指向同一个对象;

1.2 深拷贝

2.深拷贝:拷贝对象在堆内存中 new 出一个源对象,在堆内存指向两个不同的对象,拷贝对象和源对象是两个独立的对象,互相隔离。

1.3 操作步骤

1.实现Cloneable接口,重写clone方法

二 案例操作

2.1  浅拷贝

1.boss代码

public class Boss implements Cloneable{
    private String bossName;
    private String title;

    public Boss() {
    }

    public Boss(String bossName, String title) {
        this.bossName = bossName;
        this.title = title;
    }

    public String getBossName() {
        return bossName;
    }

    public void setBossName(String bossName) {
        this.bossName = bossName;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

//    @Override
//    protected Object clone() throws CloneNotSupportedException
//    {
//        return super.clone();
//    }



}

2.Emp代码

1截图

2.代码

public class Emp  implements Cloneable{
    private String empName;
    private Integer age;

    private Boss boss;

    public Emp() {
    }

    public Emp(String empName, Integer age, Boss boss) {
        this.empName = empName;
        this.age = age;
        this.boss = boss;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Boss getBoss() {
        return boss;
    }

    public void setBoss(Boss boss) {
        this.boss = boss;
    }

    public Emp(String empName, Integer age, String bossName, String title)
    {
        this.empName = empName;
        this.age = age;
        this.boss = new Boss(bossName,title);
    }
    /**
    * @author admin
    * @description       浅拷贝
    * @date 2024/7/14 9:14
    * @param []
    * @return java.lang.Object
    */
    protected Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }

//    /**
//    * @author admin
//    * @description     深拷贝
//    * @date 2024/7/14 9:14
//    * @param []
//    * @return java.lang.Object
//    */
//    @Override
//    protected Object clone() throws CloneNotSupportedException
//    {
//        return new Emp(empName,age,boss.getBossName(),boss.getTitle());
//    }
}

3.测试验证

2.2  深拷贝

1.boss代码

public class Boss implements Cloneable{
    private String bossName;
    private String title;

    public Boss() {
    }

    public Boss(String bossName, String title) {
        this.bossName = bossName;
        this.title = title;
    }

    public String getBossName() {
        return bossName;
    }

    public void setBossName(String bossName) {
        this.bossName = bossName;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

//    @Override
//    protected Object clone() throws CloneNotSupportedException
//    {
//        return super.clone();
//    }



}

2.emp代码

关键点:

代码:

public class Emp  implements Cloneable{
    private String empName;
    private Integer age;

    private Boss boss;

    public Emp() {
    }

    public Emp(String empName, Integer age, Boss boss) {
        this.empName = empName;
        this.age = age;
        this.boss = boss;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Boss getBoss() {
        return boss;
    }

    public void setBoss(Boss boss) {
        this.boss = boss;
    }

    public Emp(String empName, Integer age, String bossName, String title)
    {
        this.empName = empName;
        this.age = age;
        this.boss = new Boss(bossName,title);
    }
    /**
    * @author admin
    * @description       浅拷贝
    * @date 2024/7/14 9:14
    * @param []
    * @return java.lang.Object
    */
//    protected Object clone() throws CloneNotSupportedException
//    {
//        return super.clone();
//    }

    /**
    * @author admin
    * @description     深拷贝
    * @date 2024/7/14 9:14
    * @param []
    * @return java.lang.Object
    */
    @Override
    protected Object clone() throws CloneNotSupportedException
    {
        return new Emp(empName,age,boss.getBossName(),boss.getTitle());
    }
}

3.测试查看

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值