当前位置: 移动技术网 > IT编程>开发语言>Java > 荐 java:深拷贝与浅拷贝

荐 java:深拷贝与浅拷贝

2020年07月12日  | 移动技术网IT编程  | 我要评论

概念引入:引用拷贝
二者的引用是同一个对象,并没有创建出一个新的对象
因为是同一个对象的引用,所以两者改一个,另一个对象的值也随之改变。
在这里插入图片描述
引用拷贝包括浅拷贝与深拷贝

浅拷贝(shallowCopy):复制基本类型的属性;引用类型的属性复制,复制栈中的变量 和 变量指向堆内存中的对象的指针,不复制堆内存中的对象;
深拷贝(deepCopy):复制基本类型的属性;引用类型的属性复制,复制栈中的变量 和 变量指向堆内存中的对象的指针和堆内存中的对象
在这里插入图片描述
在这里插入图片描述
浅拷贝测试代码:

class Teacher implements Cloneable{
   String name;

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

    public Teacher(String name){this.name=name;}

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Teacher t1=(Teacher)super.clone();
        return t1;
    }
}
class Student implements Cloneable{
    public String name;
    public Teacher teacher;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student s1=(Student)super.clone();
        return s1;
    }
}
 @Test
    public void test4() throws CloneNotSupportedException {
      //初始化
        Teacher teacher1=new Teacher("teacher");
        Student student1=new Student();
        student1.setName("studenet1");
        student1.setTeacher(teacher1);
        //克隆
        Student student2=(Student)student1.clone();
        System.out.println(student1);
        System.out.println(student2);
        System.out.println("修改前student1.name:"+student1.getName());
        System.out.println("修改前student2.name:"+student2.getName());
        //修改student2.name
        student2.setName("student2");
        System.out.println("修改后student1.name:"+student1.getName());
        System.out.println("修改后student2.name:"+student2.getName());
        System.out.println(student1.getTeacher());
        System.out.println(student2.getTeacher());
    }

测试结果:
在这里插入图片描述
结果分析:
student1.clone()以后,student1与student2的地址不一样,修改student1.name,student2.name不会改变,这不就是深拷贝了吗?其实不是,引用student1对teacher的引用与student2对teacher的引用地址是一样的!?
在student类定义时,实现了Cloneable接口,并重写了Clone方法

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

我们只对student这个类进行了clone,stuednt在内存中会有2份,可是为什么teacher只有一份?
带着这个疑问我们对student重写clone方法进行修改
深拷贝测试代码

@Override
    protected Object clone() throws CloneNotSupportedException {
        Student s1=(Student)super.clone();
        s1.teacher=(Teacher)this.teacher.clone();
        return s1;
    }
```java

这么做就要在super.clone的基础上 继续对非基本类型的对象递归的再次clone.

测试结果:
在这里插入图片描述
stduent1与stduent2的地址不同,内存中有2份,stuent1.teacher与student2.teacher的地址不同,内存中也有2份,修改student1.name不影响stduent2.name,修改student.teacher.name不影响stduent2.teacher.name,实现了深拷贝,需要在super.clone的基础上 继续对非基本类型的对象递归的再次clone

我的愚解,如果有出入,欢迎指正ヾ(゚∀゚ゞ)

本文地址:https://blog.csdn.net/dll175/article/details/107280085

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网