深拷贝和浅拷贝

文章目录

Java 中内置了一些很有用的接口, Clonable 就是其中之一。
Object 类中存在一个 clone 方法,调用这个方法可以创建一个对象的 “拷贝”,但是要想合法调用 clone 方法,必须要先实现 Clonable 接口,否则就会抛出 CloneNotSupportedException 异常。

image.png
Cloneable 是一个空接口,作用是:表示当前对象是可以被克隆的。

class Student implements Cloneable {
    public String name;

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

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

public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();
        Student student2 = (Student) student1.clone();
        System.out.println(student1);
        System.out.println(student2);
        System.out.println(student1 == student2);    // false
    }
}

【浅拷贝 VS 深拷贝】:
Cloneable 拷贝出的对象是一份 “浅拷贝”。

浅拷贝: 将原对象/原数组的引用直接赋给新对象/新数组,新对象/新数组只是原对象/原数组的一个引用。(修改新对象/新数组会改变原对象/原数组);
深拷贝: 创建一个新对象/新数组,将原对象/原数组的各项属性的“值”(数组的所有元素)拷贝过来,是“值”而不是“引用”。(修改新对象/新数组不会改变原对象/原数组)

class Money {
    public double money = 9.9;
}

class Student implements Cloneable {
    public String name;
    public Money m = new Money();

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

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

public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();
        Student student2 = (Student) student1.clone();
        System.out.println("student修改前的结果:");
        System.out.println("student1: " + student1.m.money);
        System.out.println("student2: " + student2.m.money);
        System.out.println("student修改后的结果:");
        student2.m.money = 99.9;
        System.out.println("student1: " + student1.m.money);
        System.out.println("student2: " + student2.m.money);
    }
}

// 执行结果
student修改前的结果:
student1: 9.9
student2: 9.9
student修改后的结果:
student1: 99.9
student2: 99.9

image.png
如上代码,我们可以看到,通过clone,我们只是拷贝了 Student 对象,但是 Student 对象中的 Money 对象并没有拷贝。通过 student2 这个引用修改了 money 的值后,student2 这个引用访问 money 的时候,值也发生了改变。这里就是发生了浅拷贝。那如何实现深拷贝呢?
image.png

class Money implements Cloneable {
    public double money = 9.9;

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

class Student implements Cloneable {
    public String name;
    public Money m = new Money();

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

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // return super.clone();
        Student student = (Student) super.clone();
        student.m = (Money) this.m.clone();
        return student;
    }
}

public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();
        Student student2 = (Student) student1.clone();
        System.out.println("student修改前的结果:");
        System.out.println("student1: " + student1.m.money);
        System.out.println("student2: " + student2.m.money);
        System.out.println("student修改后的结果:");
        student2.m.money = 99.9;
        System.out.println("student1: " + student1.m.money);
        System.out.println("student2: " + student2.m.money);
    }
}

// 执行结果
student修改前的结果:
student1: 9.9
student2: 9.9
student修改后的结果:
student1: 9.9
student2: 99.9
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值