浅拷贝和深拷贝的区别?

创建Java对象的方式包括new、反射、反序列化、拷贝,那么什么是拷贝呢?浅拷贝和深拷贝又有什么区别呢?

什么是拷贝

拷贝就是为了复用原对象的部分或全部数据,在原对象的基础上通过复制的方式创建一个新的对象。

Object类中有native类型的clone方法

protected native Object clone() throws CloneNotSupportedException;

如果一个对象可以被拷贝,那么该对象对应的类需要实现Cloneable接口,并重写Object的clone方法。

必须实现Cloneable接口,否则调用clone方***抛出java.lang.CloneNotSupportedExecption异常

//Cloneable接口中没有任何参数或抽象方法,该接口只是一个标识 public interface Cloneable {
}
//如Student类,实现Cloneable接口,重写Object的clone方法 public class Student implements Cloneable{ private String sname; private Integer sage;

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

举个栗子

public class cloneObject { public static void main(String[] args) throws CloneNotSupportedException {
        Student student = new Student(,18);
        System.out.println("clone前的Student对象为:"+student);
        Student cloneStudent = student.clone();
        System.out.println("clone后的Student对象为:"+cloneStudent);
        System.out.println("Student对象与cloneStudent对象是否相等:"+student.equals(cloneStudent));
    }
}

打印结果为

clone前的Student对象为:Student{sname=clone后的Student对象为:Student{sname=false 

由此可见,拷贝是创建了一个新的对象。

什么是浅拷贝?

我稍等改动下Student类来举例说明

Student类中除了String类型和Integer类型的属性外,添加了一个Teacher类型的属性,Teacher类中只有一个String类型的tname属性

public class Student implements Cloneable{ private String sname; private Integer sage; private Teacher teacher;

    ... @Override protected Student clone() throws CloneNotSupportedException { return (Student) super.clone();
    }
public class Teacher { private String tname;

再次调用sutdent对象的clone方法

public static void main(String[] args) throws CloneNotSupportedException {
    Teacher teacher = new Teacher("罗翔老师");
    Student student = new Student(,18,teacher);
    System.out.println("clone前的Student对象为:"+student);
    Student cloneStudent = student.clone();
    System.out.println("clone后的Student对象为:"+cloneStudent);
    System.out.println("Student对象与cloneStudent对象是否相等:"+student.equals(cloneStudent));
    System.out.println("student对象和cloneStudent对象中的teacher属性是否相等:"+student.getTeacher().equals(cloneStudent.getTeacher()));
}

两个sutdent对象比较结果在意料之中,为false。但是两种student对象中的teacher属性比较结果却是true。

clone前的Student对象为:Student{sname=18, teacher=com.hard.qz.clone.Teacher@1b6d3586} clone后的Student对象为:Student{sname=18, teacher=com.hard.qz.clone.Teacher@1b6d3586}
Student对象与cloneStudent对象是否相等:false student对象和cloneStudent对象中的teacher属性是否相等:true 
//修改student对象中teacher属性的值,观察cloneStudent对象中的teacher属性的值是否会变 student.getTeacher().setTname("张三老师");
System.out.println("cloneStudent对象中teacher属性的值为:"+cloneStudent.getTeacher());

结果为:

cloneStudent对象中teacher属性的值为:Teacher{tname='张三老师'}
当修改student对象中teacher属性的值时,会影响到cloneStudent对象中teacher属性的值 由此说明:两个student对象共用一个teacher对象。

像上面这种情况,当拷贝时,只拷贝了部分属性,没有拷贝Object和数组属性时,这种拷贝被称为浅拷贝。

什么是深拷贝?

当拷贝时,拷贝了一个对象中的所有属性,包括Object和数组属性,这种拷贝被称为深拷贝

如何实现深拷贝?

重写clone方法时,对Object和数组属性也调用clone方法就可以实现深拷贝,要求被拷贝对象的Object属性也实现Cloneable接口,重写了clone方法。

//Teacher类实现了Cloneable接口,重写了clone方法 public class Teacher implements Cloneable{ private String tname; @Override protected Object clone() throws CloneNotSupportedException { return super.clone();
    }
} //Student类重写clone方法时,调用了Teacher类的clone方法,实现深拷贝 @Override protected Student clone() throws CloneNotSupportedException {
    Student cloneStudent = (Student) super.clone();
    cloneStudent.setTeacher((Teacher) cloneStudent.getTeacher().clone()); return cloneStudent;
}

对student对象拷贝并比较两个对象及对象中的属性是否相等。

public class cloneObject { public static void main(String[] args) throws CloneNotSupportedException {
        Teacher teacher = new Teacher("罗翔老师");
        Student student = new Student(,18,teacher);
        System.out.println("clone前的Student对象为:"+student);
        Student cloneStudent = student.clone();
        System.out.println("clone后的Student对象为:"+cloneStudent);
        System.out.println("Student对象与cloneStudent对象是否相等:"+student.equals(cloneStudent));
        System.out.println("student对象和cloneStudent对象中的teacher属性是否相等:"+student.getTeacher().equals(cloneStudent.getTeacher())); //修改student对象中teacher属性的值,观察cloneStudent对象中的teacher属性的值是否会变 student.getTeacher().setTname("张三老师");
        System.out.println("cloneStudent对象中teacher属性的值为:"+cloneStudent.getTeacher());
    }
}

打印结果为

clone前的Student对象为:Student{sname='罗翔老师'}} clone后的Student对象为:Student{sname='罗翔老师'}}
Student对象与cloneStudent对象是否相等:false student对象和cloneStudent对象中的teacher属性是否相等:false cloneStudent对象中teacher属性的值为:Teacher{tname='罗翔老师'}

打印结果显示深拷贝后的两个对象及其中的属性都不相等。


总结

  1. 拷贝是为了部分或全部复用原对象的属性,在元对象的基础上创建一个新的对象。
  2. 浅拷贝是拷贝后两个对象地址不相等,但两个对象的部分属性地址相等,新对象并没有拷贝所有的属性,而是复用原对象中的值。
  3. 深拷贝是拷贝后两个对象不仅地址不相等,两个对象的所有属性地址都不相等。
#Java##程序员##计算机##编程##面试#
全部评论
才知道还有浅拷贝和深拷贝这说,感谢楼主
点赞 回复 分享
发布于 2022-07-27 16:46

相关推荐

emmm别问我为啥上一条帖子隔了两个月我才开始投简历和拿offer,因为我懒😰简单流程如下:周一凌晨改好的简历,然后到处乱投简历;周二接到了三维家的一面通知,临时抱佛脚的背了一些八股;周三上午一面下午通知第二天hr面;周四上午hr面下午拿offer,遂收手支线:在BOSS上顺手投了几个大厂,投字节的时候不小心投城客户端了,结果过了一天HR突然把我简历要走了,还问我能不能整客户端,我直接一口答应(脏面评警告😢)结果在周三下午的时候给我打电话,说前端有空缺实习岗,问我有没有兴趣,然后就跟我约了周四下午一面😰我都没咋准备啊,咩都不会啊😭结果周四下午面完,晚上打电话通知过一面了,赶紧把二面约在下周一下午,留点缓冲时间。逆大天了,我一半的问题都不会,他居然给我过了?运气未免有点好了😥现在正在恶补计网、网安、性能优化的东西(这三大板块我是几乎一点不会,一面几乎一点答不出来,加上我又没怎么背八股,这块被干烂了😵)心得体会与经验:1. 我giao怎么这么快就结束了,我还以为要找好久😨2. 大厂的面试问题真的和中厂小厂很大不同,比如在三维家我能自己吹水到vue的数据劫持、Proxy代理响应式之类的他们就觉得很不错了,但是在字节你但凡敢提到一下就会追问你细节了,一追问马脚就全漏出来了3. 有信心真的很重要,我感觉我能拿中厂offer最重要的就是吹水吹出自信来了,以至于三维家面试反问面试官有哪里还需要改进的时候,他就说很不错了解的很多😦4. 理解很重要,我从头到尾真没背过很多八股,不过有一些知识确实是敲过代码验证过,所以面试的时候能吹水吹得出来😇想了解面经啥的可以直接评论区问我,但我可能也说不全,因为我没有记录,而且今天摆了一天感觉记忆快清空了😵下面是故事时间:我暑假刚开始的时候才开始准备八股,印象很深那个时候连什么原型、事件循环、闭包这些名词都没听过,资料也不知道怎么找,就一直零零散散的准备,感觉也只有js稍微背了一下八股,其他很多时候都是靠完全理解和手写熟悉一些机制的,但这样做效率很低,反正准备了一个多星期半个月就开摆了😭结果一摆就摆到了开学,笔记是乱七八糟的,八股是忘光光的,简历是一直没改的,实习也是一直没投过的。直到上周日晚上偶然和师兄聊天,他突然问我“你怎么还不找实习”,那天晚上才幡然醒悟,是时候做点事情了😡然后就按照上面描述的来走了。其实我感觉我从头到尾都没背特别多八股,也没怎么找刷题资料啥的,早期就是翻尚硅谷或者黑马的入门视频从头学起,中期用面试鸭看了一点点题,主要是在学js机制和敲js代码,后期才发现了w3c的面经网站,然后在那里看着学(那个时候已经懒得敲了,因为有些问题与代码感觉不像是给找实习的看的,忒细了点😂)接下来继续准备字节二面吧,虽然几乎没啥可能可以通过,但是万一有奇迹呢?😍😍😍也祝大家能够早日拿到心仪的offer
我的offer呢😡:我已经预见10天后你会发,节孝子启动了
投递三维家等公司10个岗位
点赞 评论 收藏
分享
09-22 22:22
中山大学 Java
乌鱼子萨奇:羡慕你啊,直接转正了,都不用经历秋招的炼狱,但是你少经历了很多痛苦的事情啊
点赞 评论 收藏
分享
评论
1
11
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务