Teacher teacher = new Teacher("Taylor",26); Teacher otherteacher = teacher; System.out.println(teacher); System.out.println(otherteacher);
blog.Teacher@355da254 blog.Teacher@355da254
Teacher teacher = new Teacher("Swift",26); Teacher otherteacher = (Teacher)teacher.clone(); System.out.println(teacher); System.out.println(otherteacher);
blog.Teacher@355da254 blog.Teacher@4dc63996
package com.test; public class ShallowCopy { public static void main(String[] args) throws CloneNotSupportedException { Teacher teacher = new Teacher(); teacher.setName("riemann"); teacher.setAge(27); Student2 student1 = new Student2(); student1.setName("edgar"); student1.setAge(18); student1.setTeacher(teacher); Student2 student2 = (Student2) student1.clone(); System.out.println("拷贝后"); System.out.println(student2.getName()); System.out.println(student2.getAge()); System.out.println(student2.getTeacher().getName()); System.out.println(student2.getTeacher().getAge()); System.out.println("修改老师的信息后-------------"); // 修改老师的信息 teacher.setName("Games"); System.out.println(student1.getTeacher().getName()); System.out.println(student2.getTeacher().getName()); } } class Teacher implements Cloneable { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } class Student2 implements Cloneable { private String name; private int age; private Teacher teacher; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Object clone() throws CloneNotSupportedException { Object object = super.clone(); return object; } }
拷贝后 edgar 18 riemann 27 修改老师的信息后------------- Games Games
package com.test; public class DeepCopy { public static void main(String[] args) throws CloneNotSupportedException { Teacher2 teacher = new Teacher2(); teacher.setName("riemann"); teacher.setAge(27); Student3 student1 = new Student3(); student1.setName("edgar"); student1.setAge(18); student1.setTeacher(teacher); Student3 student2 = (Student3) student1.clone(); System.out.println("拷贝后"); System.out.println(student2.getName()); System.out.println(student2.getAge()); System.out.println(student2.getTeacher().getName()); System.out.println(student2.getTeacher().getAge()); System.out.println("修改老师的信息后-------------"); // 修改老师的信息 teacher.setName("Games"); System.out.println(student1.getTeacher().getName()); System.out.println(student2.getTeacher().getName()); } } class Teacher2 implements Cloneable { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Object clone() throws CloneNotSupportedException { return super.clone(); } } class Student3 implements Cloneable { private String name; private int age; private Teacher2 teacher; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Teacher2 getTeacher() { return teacher; } public void setTeacher(Teacher2 teacher) { this.teacher = teacher; } public Object clone() throws CloneNotSupportedException { // 浅复制时: // Object object = super.clone(); // return object; // 改为深复制: Student3 student = (Student3) super.clone(); // 本来是浅复制,现在将Teacher对象复制一份并重新set进来 student.setTeacher((Teacher2) student.getTeacher().clone()); return student; } }
拷贝后 edgar 18 riemann 27 修改老师的信息后------------- Games riemann