java clone 中的浅复制和深复制
来源:互联网 发布:电脑自带编程软件 编辑:程序博客网 时间:2024/04/28 12:48
什么是浅复制?
首先,浅复制会创建一个新对象,这个新的对象各个字段的值会从原始对象复制过来,如果某个字段是引用其他的对象,那么仅仅复制此对象在内存中的引用地址。
图1
如图1 所示,对象 MainObject1 有一个 int 类型的字段 Field1 和一个字段引用 ContainObject1对象,被克隆的新对象是 MainObject2, MainObject2 有一个 int 类型字段 Field2, Field2 的值是从对象MainObject1的字段 Field1 复制过来,而MainObject2的一个字段和MainObject1 中的某个字段仍然指向同一个对象ContainObject1,也就是说只要ContainObject1 发生任何变化,MainObject1 和MainObject2所引用的ContainObject1 都会用变化。
什么是深复制?
深复制也会创建一个新的对象,除了复制这个新对象里的原始类型字段的值,还要对此对象的引用字段再做克隆,而不是仅仅复制此引用字段再内存中的引用地址。
图2
如图2 所示,MainObject1 是原始对象,MainObject2 是被克隆出的对象,从图2和图1的差别可以看出,克隆对象MainObject2 字段ContainObject2和 MainObject1 里的字段 ContainObject1 指向的是不同的对象,也就是说,MainObject1 里的 ContainObject1 发生任何变化,都不会影响到 MainObject2 里的 ContainObject2。
用 java 实现浅复制示例:
class Subject { private String name; public String getName() { return name; } public void setName(String s) { name = s; } public Subject(String s) { name = s; }}class Student implements Cloneable { //Contained object private Subject subj; private String name; public Subject getSubj() { return subj; } public String getName() { return name; } public void setName(String s) { name = s; } public Student(String s, String sub) { name = s; subj = new Subject(sub); } public Object clone() { //shallow copy try { return super.clone(); } catch (CloneNotSupportedException e) { return null; } }}public class CopyTest { public static void main(String[] args) { //Original Object Student stud = new Student("John", "Algebra"); System.out.println("Original Object: " + stud.getName() + " - " + stud.getSubj().getName()); //Clone Object Student clonedStud = (Student) stud.clone(); System.out.println("Cloned Object: " + clonedStud.getName() + " - " + clonedStud.getSubj().getName()); stud.setName("Dan"); stud.getSubj().setName("Physics"); System.out.println("Original Object after it is updated: " + stud.getName() + " - " + stud.getSubj().getName()); System.out.println("Cloned Object after updating original object: " + clonedStud.getName() + " - " + clonedStud.getSubj().getName()); }}输出结果是:
Original Object: John - AlgebraCloned Object: John - AlgebraOriginal Object after it is updated: Dan - PhysicsCloned Object after updating original object: John - Physics
用java 实现深复制示例
只需要把上例的Student类的clone 方法修改如下:
class Student implements Cloneable { //Contained object private Subject subj; private String name; public Subject getSubj() {return subj; } public String getName() {return name; } public void setName(String s) {name = s; } public Student(String s, String sub) {name = s;subj = new Subject(sub); } public Object clone() {//Deep copyStudent s = new Student(name, subj.getName());return s; }}修改后运行结果如下:
Original Object: John - AlgebraCloned Object: John - AlgebraOriginal Object after it is updated: Dan - PhysicsCloned Object after updating original object: John - Algebra
使用序列化实现深复制
请看如下示例:
public class ColoredCircle implements Serializable { private int x; private int y; public ColoredCircle(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } @Override public String toString() { return "x=" + x + ",y=" + y; }}
public class DeepCopy { static public void main(String[] args) { ObjectOutputStream oos = null; ObjectInputStream ois = null; try { // create original serializable object ColoredCircle c1 = new ColoredCircle(100, 100); // print it System.out.println("Original = " + c1); ColoredCircle c2 = null; // deep copy ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); // serialize and pass the object oos.writeObject(c1); oos.flush(); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ois = new ObjectInputStream(bin); // return the new object c2 = (ColoredCircle) ois.readObject(); // verify it is the same System.out.println("Copied = " + c2); // change the original object's contents c1.setX(200); c1.setY(200); // see what is in each one now System.out.println("Original = " + c1); System.out.println("Copied = " + c2); } catch (Exception e) { System.out.println("Exception in main = " + e); } finally { try { oos.close(); ois.close(); } catch (IOException e) { System.out.println(e); } } }}
程序输出结果:
Original = x=100,y=100Copied = x=100,y=100Original = x=200,y=200Copied = x=100,y=100
- java clone 中的浅复制和深复制
- java中的clone:深复制和浅复制
- java中的clone(深复制和浅复制)
- Java浅复制和深度复制 clone()
- Java中Clone方法的浅复制和深复制
- Java中clone方法以及深复制和浅复制
- Java Object的clone方法浅复制和深复制
- 深复制和浅复制(clone)
- PHP Clone 深复制和浅复制
- clone()方法、深复制和浅复制
- 复制对象clone(深复制和浅复制)
- java中的深复制和浅复制
- java中的深复制和浅复制
- java中的深复制和浅复制
- java中的深复制和浅复制
- Java中的深复制和浅复制
- 浅复制 深复制 clone
- Java克隆clone的浅复制与深复制
- qwt 编程使用(一)
- Android,ListView的使用
- 8瓶酒一瓶有毒,用人测试。每次测试结果8小时后才会得出,而你只有8个小时的时间。
- HDU 1535 Invitation Cards(有向图单源最短路径+SPFA)
- FCKeditor的js验证以及FCKeditor内容是否为空判断
- java clone 中的浅复制和深复制
- IOS开发(83)之为形状增加阴影
- ORACLE dblink数据库连接 使用简单字符串方式
- 解读vs2003、vs2005、vs2008开发Windows CE环境、默认SDK开发包及测试
- js判断FCKeditor内容是否为空
- [Android] 获取Android设备的唯一识别码|设备号|序号|UUID
- MFC中变量的命名方法
- DBhelper数据操作类
- 我的失恋