ToB企服应用市场:ToB评测及商务社交产业平台

标题: 面试题:深拷贝、浅拷贝、引用拷贝的区别 [打印本页]

作者: 渣渣兔    时间: 2022-9-16 17:23
标题: 面试题:深拷贝、浅拷贝、引用拷贝的区别
目录

作者:小牛呼噜噜 | https://xiaoniuhululu.com
计算机内功、JAVA底层、面试相关资料等更多精彩文章在公众号「小牛呼噜噜 」
引用拷贝

引用拷贝: 引用拷贝不会在堆上创建一个新的对象,只 会在栈上生成一个新的引用地址,最终指向依然是堆上的同一个对象
  1. //实体类
  2. public class Person{
  3.     public String name;//姓名
  4.     public int height;//身高
  5.     public StringBuilder something;
  6.     public String getName() {
  7.         return name;
  8.     }
  9.     public void setName(String name) {
  10.         this.name = name;
  11.     }
  12.     public int getHeight() {
  13.         return height;
  14.     }
  15.     public void setHeight(int height) {
  16.         this.height = height;
  17.     }
  18.     public StringBuilder getSomething() {
  19.         return something;
  20.     }
  21.     public void setSomething(StringBuilder something) {
  22.         this.something = something;
  23.     }
  24.     public Person(String name, int height, StringBuilder something) {
  25.         this.name = name;
  26.         this.height = height;
  27.         this.something = something;
  28.     }
  29. }
  30. //测试类
  31. public class copyTest {
  32.     public static void main(String[] args) {
  33.         Person p1 = new Person("小张", 180, new StringBuilder("今天天气很好"));
  34.         Person p2 = p1;
  35.         System.out.println("对象是否相等:"+ (p1 == p2));
  36.         System.out.println("p1 属性值=" + p1.getName()+ ","+ p1.getHeight() + ","+ p1.getSomething());
  37.         System.out.println("p2 属性值=" + p2.getName()+ ","+ p2.getHeight() + ","+ p2.getSomething());
  38.         // change
  39.         p1.name="小王";
  40.         p1.height = 200;
  41.         p1.something.append(",适合出去玩");
  42.         System.out.println("...after p1 change....");
  43.         System.out.println("p1 属性值=" + p1.getName()+ ","+ p1.getHeight() + ","+ p1.getSomething());
  44.         System.out.println("p2 属性值=" + p2.getName()+ ","+ p2.getHeight() + ","+ p2.getSomething());
  45.     }
  46. }
复制代码
结果:
对象是否相等:true
p1 属性值=小张,180,今天天气很好
p2 属性值=小张,180,今天天气很好
...after p1 change....
p1 属性值=小王,200,今天天气很好,适合出去玩
p2 属性值=小王,200,今天天气很好,适合出去玩
before change:

after change:

我们可以看出 由于2个引用p1,p2 都是指向堆中同一个对象,所以2个对象是相等的,修改了对象p1,会影响到对象p2
需要注意的
注意与这篇文章得区分开来 https://mp.weixin.qq.com/s/6qRspyLAsoBxttGwGtxsAA, int num1 = 10;是基本类型的局部变量,存放在栈中
浅拷贝

浅拷贝 :浅拷贝会在堆上创建一个新的对象,新对象和原对象不等,但是新对象的属性和老对象相同
其中:
如何实现浅拷贝呢?也很简单,就是在需要拷贝的类上实现Cloneable接口并重写其clone()方法
  1. @Override protected Object clone() throws CloneNotSupportedException {   
  2.     return super.clone();
  3. }
复制代码
在使用的时候直接调用类的clone()方法即可
  1. //实体类 继承Cloneable
  2. public class Person implements Cloneable{
  3.     public String name;//姓名
  4.     public int height;//身高
  5.     public StringBuilder something;
  6.     public String getName() {
  7.         return name;
  8.     }
  9.     public void setName(String name) {
  10.         this.name = name;
  11.     }
  12.     public int getHeight() {
  13.         return height;
  14.     }
  15.     public void setHeight(int height) {
  16.         this.height = height;
  17.     }
  18.     public StringBuilder getSomething() {
  19.         return something;
  20.     }
  21.     public void setSomething(StringBuilder something) {
  22.         this.something = something;
  23.     }
  24.     public Person(String name, int height, StringBuilder something) {
  25.         this.name = name;
  26.         this.height = height;
  27.         this.something = something;
  28.     }
  29.     @Override
  30.     public Person clone() throws CloneNotSupportedException {
  31.         return (Person) super.clone();
  32.     }
  33. }
  34. //测试类
  35. public class shallowCopyTest {
  36.     public static void main(String[] args) throws CloneNotSupportedException {
  37.         Person p1 = new Person("小张", 180, new StringBuilder("今天天气很好"));
  38.         Person p2 = p1.clone();
  39.         System.out.println("对象是否相等:"+ (p1 == p2));
  40.         System.out.println("p1 属性值=" + p1.getName()+ ","+ p1.getHeight() + ","+ p1.getSomething());
  41.         System.out.println("p2 属性值=" + p2.getName()+ ","+ p2.getHeight() + ","+ p2.getSomething());
  42.         // change
  43.         p1.setName("小王");
  44.         p1.setHeight(200);
  45.         p1.getSomething().append(",适合出去玩");
  46.         System.out.println("...after p1 change....");
  47.         System.out.println("p1 属性值=" + p1.getName()+ ","+ p1.getHeight() + ","+ p1.getSomething());
  48.         System.out.println("p2 属性值=" + p2.getName()+ ","+ p2.getHeight() + ","+ p2.getSomething());
  49.     }
  50. }
复制代码
结果:
对象是否相等:false
p1 属性值=小张,180,今天天气很好
p2 属性值=小张,180,今天天气很好
...after p1 change....
p1 属性值=小王,200,今天天气很好,适合出去玩
p2 属性值=小张,180,今天天气很好,适合出去玩
before change:

after change:

我们可以看出:
深拷贝

深拷贝 :完全拷贝⼀个对象,在堆上创建一个新的对象,拷贝被拷贝对象的成员变量的值,同时堆中的对象也会拷贝。
需要重写clone方法
  1.     @Override
  2.     public Person clone() throws CloneNotSupportedException {
  3.         //return (Person) super.clone();
  4.         Person person = (Person) super.clone();
  5.         person.setSomething( new StringBuilder(person.getSomething()));//单独为引用类型clone
  6.         return person;
  7.     }
复制代码
shallowCopyTest测试类的结果:
对象是否相等:false
p1 属性值=小张,180,今天天气很好
p2 属性值=小张,180,今天天气很好
...after p1 change....
p1 属性值=小王,200,今天天气很好,适合出去玩
p2 属性值=小张,180,今天天气很好
这时候对象p1和对象p2互不干扰了
before change:

after change:

但这样也有个小问题,对象每有一个引用类型,我们都得重写其clone方法,这样会非常麻烦,因此我们还可以借助序列化来实现对象的深拷贝
  1. //实体类 继承Cloneable
  2. public class Person implements Serializable{
  3.     public String name;//姓名
  4.     public int height;//身高
  5.     public StringBuilder something;
  6. ...//省略 getter setter
  7.     public Object deepClone() throws Exception{
  8.         // 序列化
  9.         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  10.         ObjectOutputStream oos = new ObjectOutputStream(bos);
  11.    
  12.         oos.writeObject(this);
  13.    
  14.         // 反序列化
  15.         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  16.         ObjectInputStream ois = new ObjectInputStream(bis);
  17.    
  18.         return ois.readObject();
  19.     }
  20. }
  21. //测试类,这边类名笔者就不换了,在之前的基础上改改
  22. public class shallowCopyTest {
  23.     public static void main(String[] args) throws Exception {
  24.         Person p1 = new Person("小张", 180, new StringBuilder("今天天气很好"));
  25.         Person p2 = (Person)p1.deepClone();
  26.         System.out.println("对象是否相等:"+ (p1 == p2));
  27.         System.out.println("p1 属性值=" + p1.getName()+ ","+ p1.getHeight() + ","+ p1.getSomething());
  28.         System.out.println("p2 属性值=" + p2.getName()+ ","+ p2.getHeight() + ","+ p2.getSomething());
  29.         // change
  30.         p1.setName("小王");
  31.         p1.setHeight(200);
  32.         p1.getSomething().append(",适合出去玩");
  33.         System.out.println("...after p1 change....");
  34.         System.out.println("p1 属性值=" + p1.getName()+ ","+ p1.getHeight() + ","+ p1.getSomething());
  35.         System.out.println("p2 属性值=" + p2.getName()+ ","+ p2.getHeight() + ","+ p2.getSomething());
  36.     }
  37. }
复制代码
这样也会得到深拷贝的结果
小结

其中:
参考资料:
https://blog.csdn.net/JingLxian/article/details/106337395
https://www.cnblogs.com/hithlb/p/4872373.html
很感谢你能看到最后,如果喜欢的话,欢迎关注点赞收藏转发,谢谢!更多精彩的文章


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4