java语言——对象的深复制(deep clone)与浅复制(shallow clone)
来源:互联网 发布:香港高登淘宝店 编辑:程序博客网 时间:2024/05/18 19:45
-浅复制(浅克隆):被复制的对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象。而不复制他所引用的对象。
-深复制(深克隆):被复制的对象的所有变量都含有与原来对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换而言之,深复制把要复制的对象所引用的对象都复制了一遍。
Java的clone()方法【定义在Object类中】
-clone方法将对象复制了一份并返回给调用者。
一般而言,clone()方法满足:
-(1)对任何的对象x,都有x.clone() != x
克隆对象与原对象不是同一个对象
-(2)对任何的对象x,都有
x.clone().getClass() == x.getClass()
克隆对象与原来对象的类型一样
-(3)如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。
浅复制:
package clone;public class CloneTest2 { public static void main(String[] args) throws Throwable { Teacher teacher = new Teacher(); teacher.setAge(40); teacher.setName("Teacher zhang"); Student2 s1 = new Student2(); s1.setAge(20); s1.setName("zhangsan"); s1.setTeacher(teacher); Student2 s2 = (Student2)s1.clone(); System.out.println(s2.getTeacher().getName()); System.out.println(s2.getTeacher().getAge()); System.out.println("----------------------------------------"); teacher.setName("Teacher li"); System.out.println(s2.getTeacher().getName()); System.out.println(s2.getTeacher().getAge()); }}class Teacher implements Cloneable{ private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}class Student2 implements Cloneable{ private int age; private String name; private Teacher teacher; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); }}
深复制:
package clone;public class CloneTest2 { public static void main(String[] args) throws Throwable { Teacher teacher = new Teacher(); teacher.setAge(40); teacher.setName("Teacher zhang"); Student2 s1 = new Student2(); s1.setAge(20); s1.setName("zhangsan"); s1.setTeacher(teacher); Student2 s2 = (Student2)s1.clone(); System.out.println(s2.getTeacher().getName()); System.out.println(s2.getTeacher().getAge()); System.out.println("----------------------------------------"); teacher.setName("Teacher li"); System.out.println(s2.getTeacher().getName()); System.out.println(s2.getTeacher().getAge()); }}class Teacher implements Cloneable{ private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); }}class Student2 implements Cloneable{ private int age; private String name; private Teacher teacher; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public Object clone() throws CloneNotSupportedException { Student2 student2 = (Student2)super.clone(); student2.setTeacher((Teacher)student2.getTeacher().clone()); return student2; }}
package clone;public class CloneTest2 { public static void main(String[] args) throws Throwable { Teacher teacher = new Teacher(); teacher.setAge(40); teacher.setName("Teacher zhang"); Student2 s1 = new Student2(); s1.setAge(20); s1.setName("zhangsan"); s1.setTeacher(teacher); Student2 s2 = (Student2)s1.clone(); System.out.println(s2.getTeacher().getName()); System.out.println(s2.getTeacher().getAge()); System.out.println("----------------------------------------"); teacher.setName("Teacher li"); System.out.println(s2.getTeacher().getName()); System.out.println(s2.getTeacher().getAge()); }}class Teacher implements Cloneable{ private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); }}class Student2 implements Cloneable{ private int age; private String name; private Teacher teacher; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public Object clone() throws CloneNotSupportedException { Student2 student2 = (Student2)super.clone(); student2.setTeacher((Teacher)student2.getTeacher().clone()); return student2; }}
- java语言——对象的深复制(deep clone)与浅复制(shallow clone)
- 数组克隆及对象的深、浅克隆(deep clone、shallow clone)
- java浅拷贝(shallow clone)与深拷贝(deep clone)
- java实现shallow clone(浅克隆)与深克隆(deep clone)
- java shallow clone(浅克隆)与深克隆(deep clone) 笔记
- Java中shallow clone 与deep Clone的区别
- Java中shallow clone 与deep Clone的区别
- 复制对象clone(深复制和浅复制)
- Java Deep Clone Shallow Clone 深克隆和浅克隆
- Java Deep Clone Shallow Clone 深克隆和浅克隆
- java clone 对象的深复制(深克隆)和浅复制(浅克隆)介绍
- JAVA 浅复制(clone)
- Java克隆clone的浅复制与深复制
- Java中深拷贝(Deep Clone)与浅拷贝(Shallow Clone)
- php5对象复制、clone、浅复制与深复制
- php5对象复制、clone、浅复制与深复制
- clone()方法(浅复制、深复制)
- java中的clone(深复制和浅复制)
- 数字信号产生之ARMA模型数据的产生
- 【设置PowerPoint 2013里面图片不自行压缩】_91xcz
- S3C6410开发全纪录(一)《还原SD卡启动的真相》
- ubuntu下安装Apache+PHP+Mysql
- javascript--内置对象列表
- java语言——对象的深复制(deep clone)与浅复制(shallow clone)
- mysql 增加用户
- S3C6410开发全纪录(二)《如何计算内存大小,并在UBOOT中调整内存大小》
- KMP-删除字符串中给定的字符串
- JavaScript学习——内置属性
- Android 解决下载地址有中文空格问题
- Intel手册系统编程卷第3章 保护模式内存管理
- linux下vim命令详解
- S3C6410开发全纪录(三)《UBOOT中添加DM9000AE网卡驱动》