首页 / JAVA / java浅克隆与深克隆
java浅克隆与深克隆
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java浅克隆与深克隆,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4371字,纯文字阅读大概需要7分钟。
内容图文
- 浅克隆 通常只是对克隆的实例进行复制,但里面的其他子对象,都是共用的。
- 深克隆 克隆的时候会克隆它的子对象的引用,里面所有的变量和子对象都是又额外拷贝了一份。
?????? 浅克隆
/** * 浅克隆 * @author InJavaWeTrust * */ class Father implements Cloneable { public String name; public int age; public Father(String name, int age) { this.name = name; this.age = age; } } public class Child implements Cloneable { public String name; public int age; public Father father; public Child(String name, int age, Father father) { this.name = name; this.age = age; this.father = father; } public Object clone() { Child child = null; try { child = (Child) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return child; } public static void main(String[] args) { Father father = new Father("李刚", 44); Child child1 = new Child("李启铭", 14, father); Child child2 = (Child) child1.clone(); child2.father.name = "赵刚"; child2.father.age = 40; child2.name = "赵启铭"; child2.age = 15; System.out.println(child1.name + " " + child1.age); System.out.println(child1.father.name + " " + child1.father.age); System.out.println(child2.name + " " + child2.age); System.out.println(child2.father.name + " " + child2.father.age); } // 运行结果 // 李启铭 14 // 赵刚 40 // 赵启铭 15 // 赵刚 40 // 浅克隆时father对象都是共用的,所以father重新赋值之后,之前的值也跟着变化。 }
?????? 深克隆
/** * 深克隆 * @author InJavaWeTrust * */ class Father implements Cloneable { public String name; public int age; public Father(String name, int age) { this.name = name; this.age = age; } public Object clone() { Father father = null; try { father = (Father) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return father; } } public class Child implements Cloneable { private String name; private int age; private Father father; public Child(String name, int age, Father father) { this.name = name; this.age = age; this.father = father; } public Object clone() { Child child = null; try { child = (Child) super.clone(); child.father = (Father) father.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return child; } public static void main(String[] args) { Father father = new Father("李刚", 44); Child child1 = new Child("李启铭", 14, father); Child child2 = (Child) child1.clone(); child2.father.name = "赵刚"; child2.father.age = 40; child2.name = "赵启铭"; child2.age = 15; System.out.println(child1.name + " " + child1.age); System.out.println(child1.father.name + " " + child1.father.age); System.out.println(child2.name + " " + child2.age); System.out.println(child2.father.name + " " + child2.father.age); } // 运行结果 // 李启铭 14 // 李刚 44 // 赵启铭 15 // 赵刚 40 // 深克隆时father对象也 重新拷贝了一份,所以father重新赋值之后,之前的值不会发生变化。 }
????? 利用序列化也可以实现深克隆。
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * 序列化深克隆 * @author InJavaWeTrust * */ class Father implements Serializable { private static final long serialVersionUID = 5339188002945518918L; public String name; public int age; public Father(String name, int age) { this.name = name; this.age = age; } } public class Child implements Serializable{ private static final long serialVersionUID = 991407861345394819L; private String name; private int age; private Father father; public Child(String name, int age, Father father) { this.name = name; this.age = age; this.father = father; } public Object deepClone() { Object object = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(this); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); object = ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return object; } public static void main(String[] args) { Father father = new Father("李刚", 44); Child child1 = new Child("李启铭", 14, father); Child child2 = (Child) child1.deepClone(); child2.father.name = "赵刚"; child2.father.age = 40; child2.name = "赵启铭"; child2.age = 15; System.out.println(child1.name + " " + child1.age); System.out.println(child1.father.name + " " + child1.father.age); System.out.println(child2.name + " " + child2.age); System.out.println(child2.father.name + " " + child2.father.age); } // 运行结果 // 李启铭 14 // 李刚 44 // 赵启铭 15 // 赵刚 40 }
原文:http://injavawetrust.iteye.com/blog/2310086
内容总结
以上是互联网集市为您收集整理的java浅克隆与深克隆全部内容,希望文章能够帮你解决java浅克隆与深克隆所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】