deep clone and shallow clone 发表于 2017-08-30 | 分类于 技术 deep clone and shallow clone deep clone and shallow clone 浅复制(浅克隆):被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。 深复制(深克隆):被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把要复制的对象所引用的对象都复制一遍。 12345678910111213141516171819202122232425262728293031323334353637383940414243444546public class CloneTest1 { public static void main(String[] args) throws CloneNotSupportedException { Student student = new Student(); student.setName("bob"); student.setAge(20); Student student1 = (Student) student.clone(); System.out.println(student1.getName()); System.out.println(student1.getAge()); System.out.println("-------------------------"); student1.setName("lisi"); System.out.println(student.getName()); System.out.println(student1.getName()); }}class Student implements Cloneable { private int age; private String name; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } 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; }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990public class CloneTest2 { public static void main(String[] args) throws CloneNotSupportedException { Teacher teacher = new Teacher(); teacher.setName("teacher li"); teacher.setAge(28); Student2 student = new Student2(); student.setName("bob"); student.setAge(18); student.setTeacher(teacher); Student2 student2 = (Student2) student.clone(); student2.setTeacher((Teacher) student.getTeacher().clone()); System.out.println(student2.getName()); System.out.println(student2.getAge()); System.out.println(student2.getTeacher().getName()); System.out.println(student2.getTeacher().getAge()); student2.getTeacher().setName("teacher zhang"); System.out.println(student.getTeacher().getName()); System.out.println(student2.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 protected Object clone() throws CloneNotSupportedException { return super.clone(); }}class Student2 implements Cloneable { private int age; private String name; private Teacher teacher; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } 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; }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990public class CloneTest3 { public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException { Teacher3 teacher = new Teacher3(); teacher.setName("teacher li"); teacher.setAge(28); Student3 student = new Student3(); student.setName("bob"); student.setAge(18); student.setTeacher(teacher); Student3 student2 = (Student3) student.deepClone(); System.out.println(student2.getName()); System.out.println(student2.getAge()); System.out.println(student2.getTeacher().getName()); System.out.println(student2.getTeacher().getAge()); student2.getTeacher().setName("teacher zhang"); System.out.println(student.getTeacher().getName()); System.out.println(student2.getTeacher().getName()); }}class Teacher3 implements Serializable { 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 int age; private String name; private Teacher3 teacher; public Object deepClone() throws IOException, ClassNotFoundException { 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(); } 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; }}