???? 被clone的对象如果有属性是引用型对象,如果不复写clone方法,则clone是浅度clone。即当被clone的对象引用的对象发生改变时,clone的对象也会相应发生改变。clone的对象与被clone的对象不能保持独立。
例如:
class="java">package hashcode; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; /** * @author Administrator * */ public class Student implements Cloneable { private int age; private String name; private Student deskmate; public Student getDeskmate() { return deskmate; } public void setDeskmate(Student deskmate) { this.deskmate = deskmate; } 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 String toString() { return "Student [age=" + age + ", name=" + name + ", deskmate="+deskmate + "]"; } public Student(int age, String name) { super(); this.age = age; this.name = name; } public static void main(String[] args) throws CloneNotSupportedException { // clone Student goodStudent = new Student("goodStudent", 12); Student student1 = new Student("liuliu", 10); student1.setDeskmate(goodStudent); Student student2 = (Student) student1.clone(); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] System.out.println(student2); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] // student1's deskmate's name is changed. // student2 has the same reference, student2's deskmate's name is the // same as student 1's. // This is shallow clone. student1.getDeskmate().setName("badStudent"); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=badStudent, deskmate=null]] System.out.println(student2); // Student [age=10, name=liuliu, deskmate=Student [age=12, name=badStudent, deskmate=null]] } }
深度clone则需覆写clone方法:
@Override protected Object clone() throws CloneNotSupportedException { Student s = (Student) super.clone(); Student dm = new Student(); dm.setAge(s.getDeskmate().getAge()); dm.setName(s.getDeskmate().getName()); s.setDeskmate(dm); return s; }
?这样测试结果表明clone后的对象与被clone的对象是独立的
public static void main(String[] args) throws CloneNotSupportedException { Student goodStudent = new Student("goodStudent", 12); Student student1 = new Student("liuliu", 10); student1.setDeskmate(goodStudent); Student student2 = (Student) student1.clone(); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] System.out.println(student2); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] // student1's deskmate's name is changed. // student2 has its own reference // This is deep copy. student1.getDeskmate().setName("badStudent"); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=badStudent, deskmate=null]] System.out.println(student2); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] }
??