Java的深拷贝和浅拷贝

2023-06-04 0 266

Java的深复本和浅复本

¶ 第一类复本

在展开说深复本和浅复本以后,先来阐释阐释呵呵甚么是第一类复本。第一类复本(Object Copy)是将两个第一类的特性复本到另两个有著完全相同类类别的第一类中去。

能单纯等效为在笔记本电脑上拷贝文档,这时,拷贝一般文档和拷贝镜像就造成了差别,那个差别是接下去须要预测的深复本和浅复本的差别。

¶ 第一类复本的同时实现

在Java中假如想同时实现复本(忽视第一类间采用=号),根本无法采用clone方式。clone方式采用protect润色,新闻稿在Object上,也是大部份Object子第一类都能采用clone方式展开第一类复本。

protected native Object clone() throws CloneNotSupportedException;

Java

在注解中能看见,假如那个类没承继自CloneableUSB,所以它会放出

CloneNotSupportedException 极度。

在同时实现USB,并初始化clone时,就能顺利完成第一类的复本。

¶ 深复本和浅复本

在第一类复本段落等效笔记本电脑上拷贝文档那样,特别针对一般文档和镜像文档有相同的作法,此种作法在Java第一类复本的上的充分体现是深复本。

在 Java 中,除基本上正则表达式(元类别)以外,还存有 类的实例第一类那个引用正则表达式。假如在复本第一类的过程中,只对基本上类别的变量展开了值得拷贝,却对引用类别只做了引用的拷贝(也是内存地址引用),没真正拷贝引用到的第一类。此时的第一类复本就叫做浅复本

与之相反,不光对基本上正则表达式执行了值得拷贝,而且在拷贝引用类别拷贝时,不是仅仅传递引用,而是将引用到的第一类真正的拷贝(分配内存),此时的第一类复本就叫做深复本

¶ 深复本和浅复本的区别

其实上面在引申概念时,就已经得出深复本和浅复本的区别了,深复本不光要复本进本正则表达式的值,还要顺利完成对引用类别创建两个新的第一类,并拷贝其内的成员变量。

¶ 深复本和浅复本的实例

浅复本用例public class CloneTest implements Cloneable { public int x; public SonClone son; public int getX() { returnx; }public void setX(int x) { this.x = x; } public SonClone getSon() { return son; } public void setSon(SonClone son) { this.son = son; } @Override protected Object clone() throws CloneNotSupportedException {return super.clone(); } @Test public void testClone(){ CloneTest test = newCloneTest(); test.setX(127); test.setSon(new SonClone()); try { CloneTest clone = (CloneTest) test.clone(); // 比较test和拷贝第一类copy System.out.println(“test == clone –> “ +(test == clone)); System.out.println(“test.hash == clone.hash –> “ +(test.hashCode() == clone.hashCode())); System.out.println(“test.getClass() == clone.getClass() –> “ + (test.getClass() == clone.getClass())); System.out.println(“test.son == clone.son –> “ +(test.getSon() == clone.getSon())); System.out.println(“test.son.hash == clone.son.hash –> “ +(test.getSon().hashCode() == clone.getSon().hashCode())); } catch(CloneNotSupportedException e) { e.printStackTrace(); } }class SonClone implements Cloneable{ inta; } }

Java

浅复本的执行结果如下:

test == clone –> false test.hash == clone.hash –> false test.getClass() == clone.getClass() –>true test.son == clone.son –> true test.son.hash == clone.son.hash –> true

能看见,采用clone能拷贝第一类,第一类的hashcode已经不完全相同了,但是引用第一类却没执行拷贝第一类的过程,返回的hashcode值仍然是完全相同的,也是仅仅拷贝了引用。

深复本用例public class DeepClone implements Cloneable{ public int x; public SonClone son; public int getX() { return x; } public void setX(int x) { this.x = x; }public SonClone getSon() { return son; } public void setSon(SonClone son) { this.son = son; }class SonClone implements Cloneable{ int name; public int getName() { return name; } public void setName(int name) { this.name = name; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } @Override protected Object clone() throws CloneNotSupportedException { DeepClone clone = (DeepClone)super.clone(); clone.son = (SonClone) this.son.clone(); return clone; } @Test public void testClone(){ DeepClone test = new DeepClone(); test.setX(127); test.setSon(new SonClone()); try { DeepClone clone = (DeepClone) test.clone(); // 比较test和拷贝第一类copySystem.out.println(“test == clone –> “ +(test == clone)); System.out.println(“test.hash == clone.hash –> “+(test.hashCode() == clone.hashCode())); System.out.println(“test.getClass() == clone.getClass() –> “ + (test.getClass() == clone.getClass())); System.out.println(“test.x == clone.x –> “+(test.getX() == clone.getX())); System.out.println(“test.son == clone.son –> “+(test.getSon() == clone.getSon())); System.out.println(“test.son.hash == clone.son.hash –> “+(test.getSon().hashCode() == clone.getSon().hashCode())); }catch(CloneNotSupportedException e) { e.printStackTrace(); } } }

Java

这里深度复本能看出在父类采用clone时,会手动将clone出的父类中的引用指向拷贝clone出来的子类第一类。这时对父类执行了深复本,但实则对子类展开了一次浅复本。结果显而易见,最后的引用类别值和hashcode都不完全相同。

¶ 总结

复本第一类须要采用clone方式,并须要承继CloneableUSB,假如不手动重写clone方式,则默认会根本无法执行浅复本。

浅复本只会拷贝基本上正则表达式的值,而不会拷贝引用类别的第一类,而深复本须要手动编写clone方式来达到既能拷贝基本上数据值,又能够顺利完成对引用类别的第一类的拷贝。

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务