一、浅复制和深复制概念
浅复制(浅克隆): 被复制对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不是复制它所引用的对象。
深复制(深克隆): 被复制对象的所有变量都含有与原来对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象, 而不再试原有的那些被引用的对象。换言之,深复制把复制的对象所应用的对象都复制了一遍。
二、Java的Clone()方法 【定义在Object类中】
clone方法将对象复制一份并返回给调用者。
一般而言,clone() 方法满足:
1、对任何对象x,都有x.clone() != x
克隆对象与原对象不是一个对象
2、对任何的对象x,都有
x.clone().getClass() == x.getClass()
克隆对象与原对象的类型一样
3、如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立
二、浅复制Demo
public class CloneTest1 { public static void main(String[] args) throws CloneNotSupportedException { Student student = new Student(); student.setAge(20); student.setName("Larry"); Student student2 = (Student)student.clone(); System.out.println(student2.getAge()); System.out.println(student2.getName()); System.out.println("-----------------------"); student2.setName("Nick"); System.out.println(student.getName()); System.out.println(student2.getName()); } } class Student 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 public Object clone() throws CloneNotSupportedException { Object object = super.clone(); return object; } }
打印结果
20
zhangsan
-----------------------
zhangsan
Lisi
student2.setName("Nick"); 执行后,student2的name赋予新的值,指向新的空间。如下图
三、深复制
以下代码还是浅复制
public class CloneTest2 { public static void main(String[] args) throws CloneNotSupportedException { 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.getAge()); System.out.println(s2.getName()); teacher.setName("Teacher Li"); System.out.println(s2.getTeacher().getAge()); System.out.println(s2.getTeacher().getName()); } } class Teacher { 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 { Object object = super.clone(); return object; } }
修改成深复制
Teacher也实现Clone方法
然后改写Student的Clone方法
clone Student的时候,修改Teacher。
修改后的完整代码:
public class CloneTest2 { public static void main(String[] args) throws CloneNotSupportedException { 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.getAge()); System.out.println(s2.getName()); teacher.setName("Teacher Li"); System.out.println(s2.getTeacher().getAge()); System.out.println(s2.getTeacher().getName()); } } 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 public Object clone() throws CloneNotSupportedException { 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; } }
五、利用序列化来做深复制(最简单也是最多的一种方式)
1、使用Clone方法实现深复制存在的缺点: 存在多个引用的成员变量,将做多次处理。
如上图做了一次处理,如果有多个应用,将做多次处理。
2、序列化/反序列化
把对象写到流里的过程是序列化(Serilization)过程,而把对象从流中读取出来的过程则叫饭序列化(Deserialization)过程。应当指出的是,写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面
1)在Java语言里深复制一个对象,常常可以先使对象实现Serializable接口,然后把对象(实际上只是对象的一个拷贝)写到一个流里,再从流里读出来,便可以重建对象。
2)这样做的前提示对象以及对象内部所有引用到的对象都是可串行化的,否则,就需要仔细考察那些不可串行化的对象是否设成transient,从而将之排除在复制过程之外。
3)Code
public class CloneTest3 { public static void main(String[] args) throws Exception { Teacher3 teacher = new Teacher3(); teacher.setAge(40); teacher.setName("Teacher zhang"); Student3 s1 = new Student3(); s1.setAge(20); s1.setName("zhangsan"); s1.setTeacher(teacher); Student3 s2 = (Student3)s1.deepCopy(); System.out.println(s2.getAge()); System.out.println(s2.getName()); System.out.println("-------------"); System.out.println(s2.getTeacher().getAge()); System.out.println(s2.getTeacher().getName()); s2.getTeacher().setAge(50); s2.getTeacher().setName("Teacher Li"); System.out.println(s1.getTeacher().getAge()); System.out.println(s1.getTeacher().getName()); } } class Teacher3 implements Serializable{ //private static final long serialVersionUID = 675697793444541314L; 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 Student3 implements Serializable{ //private static final long serialVersionUID = -7008294898863009451L; private int age; private String name; private Teacher3 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 Teacher3 getTeacher() { return teacher; } public void setTeacher(Teacher3 teacher) { this.teacher = teacher; } public Object deepCopy() throws Exception { //将对象写入流 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); //从流中读取对象 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return ois.readObject(); } }
打印结果:
20 zhangsan ------------- 40 Teacher zhang 40 Teacher zhang
六、关于实现Seriallizable接口的类中的serialVersionUID问题
如果你的对象序列化后存在硬盘上面后,可是后来你更改了类的field(增加或减少或改名),当你反序列化时,就会出现Execption的,这样就会造成不兼容性的问题。
但当serialVersionUID相同时,它就会将不一样的field以type的缺省值Deserialize,这样可以避开不兼容的问题。