要想实现克隆,需要实现Cloneable接口并重写clone()方法。
浅复制,对于基本类型也会重新new一个空间来存储,而对于一个类中关联的其他类则会指复制指向那个对象的引用。例如。
public class Student { private int age = 0; public Student(int age) { super(); this.age = age; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } }
public class Test implements Cloneable { private int i; private Student student=new Student(0); public int getI() { return i; } public void setI(int i) { this.i = i; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Object clone() { Test cloneTest = null; try { cloneTest = (Test) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return cloneTest; } public void testClone() { Test t = (Test) clone(); t.setI(2); System.out.println("int i: " + t.getI()); t.getStudent().setAge(20); System.out.println("age:" + t.getStudent().getAge()); } public static void main(String[] agrs) throws Exception { Test test = new Test(); System.out.println(" clone..."); test.testClone(); System.out.println("the origal value..."); System.out.println("int i: " + test.getI()); System.out.println("age: " + test.getStudent().getAge()); }
会输出:
clone... int i: 2 age:20 the origal value... int i: 0 age: 20
可以看到我修改克隆后的Student的对象,使原来的Student对象受到影响,可知复制的是引用,而非真正的一个对象。
还有就是,int i 的值的确变了,因为是基本类型,clone的时候会简单的new 出一个空间存新的i的值。
下面看用深复制来处理:
重写Student类的clone方法,虽然Student类只有简单类型的属性,这样做其他类调用Student对象的clone()方法实现克隆,而不是 其他类.setStudent(new Student(0))的方式,前种方式对日后扩展比较好,因为只需要在Studnet类里的clone方法加代码,而不需要修改用到Student对象的类。
public class Student implements Cloneable{ private int age = 0; public Student(int age) { super(); this.age = age; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public Object clone() { Student cloneStudent = null; try { cloneStudent = (Student) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return cloneStudent; } }
修改Test类下的clone()方法:
public Object clone() { Test cloneTest = null; try { cloneTest = (Test) super.clone(); cloneTest.setStudent((Student)student.clone()); //cloneTest.setStudent(new Student(0));这样不需要重写Student类里的clone方法, //因为其只有基本类型的属性。 } catch (CloneNotSupportedException e) { e.printStackTrace(); } return cloneTest; }
再次运行后的结果为:
clone... int i: 2 age:20 the origal value... int i: 0 age: 0
Test对象clone()后,再对其student对象进行修改不会影响原始Test对象的值,因为此克隆后的Test对象在堆中已经开辟一个区域用于存储Student对象,而不是它的引用。
关联知识:
Test类中重写的clone()方法中调用了super.clone(),是因为无论clone类的继承结构是什么样的,super.clone()都会调用java.lang.Object类的clone()方法。Object类的clone()一个native方法,native方法的效率一般来说都是远高于java中的非native方法。
还有一点要考虑的是为了让其它类能调用这个clone类的clone()方法,重载之后要把clone()方法的属性设置为public。