【克隆方法+深浅拷贝】


前言

Clonable 接口

克隆方法代码的实现

//1.当类要调用克隆方法时,这个类要实现一个Cloneable接口
class Student implements Cloneable{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    //2.类要重写克隆方法时,调用克隆方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Test {
    //3.当类要调用克隆方法时,要在main方法后面添加throws CloneNotSupportedException(声明异常)
    public static void main(String[] args) throws CloneNotSupportedException{
        Student student = new Student("zhangsan",10);
        System.out.println(student);
        //克隆一个跟Student一样的对象
        Student student1 = (Student) student.clone();//4.返回值是Object,父类给子类会报错,要强转成子类,向下转型
        System.out.println(student1);

    }
}

总结:
1.当类要调用克隆方法时,这个类要implements Cloneable接口
2.在类里面要重写克隆方法
3.要在main方法后面添加 throws CloneNotSupportedException(声明异常)
4.实例化克隆对象时,因为重写方法的返回值是Object,父类给子类会报错,所以要强转成子类

浅拷贝

被克隆对象和克隆对象引用变量的内容是一样的,存储的地址也一样,当变量的值改变了,克隆对象和被克隆对象的引用指向的还是同一个。
所以浅拷贝就是,被克隆对象引用谁,克隆对象就引用谁。

类似于小时候大家基本都经历过的两个孩子看电视抢一个遥控器,孩子1要看哪个台,孩子2就必须看哪个台。

class Money{
    public double m = 19.9;
}
class Student implements Cloneable{
    public String name;
    public int age;

    //在Student类中实例化money,以便Student调用
    Money money = new Money();
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException{
        Student student = new Student("张三",10);
        Student student1 = (Student) student.clone();

        System.out.println("student "+student.money.m);
        System.out.println("student1 "+student1.money.m);

        System.out.println("==============");

        student.money.m=99.9;
        System.out.println("student "+student.money.m);
        System.out.println("student1 "+student1.money.m);
    }
}

解析图:

在这里插入图片描述

深拷贝

把被克隆对象引用的对象再克隆一份出来,让克隆的对象引用。
当被克隆对象引用的对象改变了,克隆的对象引用的对象还是原来的值。
就是两个孩子看电视一人一个遥控器,孩子1转台不会影响孩子2

class Money implements  Cloneable{
    public double m = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{
    public String name;
    public int age;

    //在Student类中实例化money,以便Student调用
    Money money = new Money();

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student tmp = (Student)super.clone();
        tmp.money = (Money) this.money.clone();
        return tmp;
    }

}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException{
        Student student = new Student("张三",10);
        Student student1 = (Student) student.clone();

        System.out.println("student "+student.money.m);
        System.out.println("student1 "+student1.money.m);

        System.out.println("==============");

        student.money.m=99.9;
        System.out.println("student "+student.money.m);
        System.out.println("student1 "+student1.money.m);

    }

解析图:
在这里插入图片描述

总结

再次学了浅拷贝和深拷贝,比上一次思路清晰多了,浅拷贝的原理和代码较容易理解,深拷贝的原理和代码较复杂,理解得还不够深入,需多看几遍巩固。
今晚脑子有点乱,希望我也能轻舟已过万重山。

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值