java clone()方法理解

来源:互联网 时间:1970-01-01

编码过程中需要用到clone方法,以前看过不少,长时间不用记得不是很清楚,于是google了一下发现好多都说的比较乱,于是重新查了下资料做个记录。
首先是java6 api中Cloneable接口的描述

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

主要部分是第一段,意思是:实现了Cloneable接口的类可以合法的使用clone方法来创建一个该类的属性对属性复制的对象。我的理解是clone方法返回一个新对象,该对象的属性值是对原对象属性值的简单复制。
下面是java6 api中对Obejct对象clone方法的注释
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression: x.clone() != xwill be true, and that the expression: x.clone().getClass() == x.getClass()will be true, but these are not absolute requirements. While it is typically the case that: x.clone().equals(x)will be true, this is not an absolute requirement.By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

大概的主要意思是
1.x.clone() != x,意思是clone方法返回的对象是新生成的对象与原来对象的地址不相等。
2. x.clone().getClass() == x.getClass(),两个对象的类是相同的。
3. x.clone().equals(x),这点需要着重说明:这个不是clone方法的绝对要求,这个主要看equals方法的实现。
4.按照惯例每个实现Cloneable接口的类的clone()方法的对象需要由super.clone()来生成,这样可以保证该类的父类(除了Object类)都满足条件2.
5.在返回由super.clone()生成的对象前需要根据需求适当的修改生成对象的属性,这就涉及到深拷贝和浅拷贝。因为Object的clone()方法只是对简单的属性值复制,所以如果对象中有可变的数据结构(类似数组,链表,对象等引用类型)需要进行深拷贝。而如果只是基本类型,或者不可变的对象(如String对象)则只需要浅拷贝。

另外需要注意的是Object.clone()方法是protect类型的,我们在覆盖的时候需要修改为public类型。
下面这段代码是具体类cloe()方法浅拷贝一般写法
abstract public class X implements Cloneable { public X clone() throws CloneNotSupportedException { return (X) super.clone(); }}

这是深拷贝的一般写法
public Z clone() throws CloneNotSupportedException { Z newZ = (Z) super.clone(); newZ.someABC = someABC.clone(); return newZ;

下面贴出测试clone方法的测试代码
import java.util.Arrays;/** * * @author lwj.charles * */public class CloneTest {/** * 测试类A */class A implements Cloneable{private int num; //不可变属性,int类型private String str;//不可变属性,String对象是不可修改的。public A(int num, String str){this.num = num;this.str = str;}public A clone() throws CloneNotSupportedException{return (A)super.clone();}public void display(){System.out.println("-----begin-----");System.out.println("int:" + num);System.out.println("String:" + str);System.out.println("-----end-----");}}/** * 测试类B */class B implements Cloneable{private int num; //不可变属性private int [] nums;//可变属性public B(int num, int[] nums){this.num = num;this.nums = nums;}///**// * clone()方法的浅拷贝实现// *///public B clone() throws CloneNotSupportedException{//return (B)super.clone();//}/** * clone()方法的深拷贝实现 */public B clone() throws CloneNotSupportedException{B b = (B)super.clone();b.nums = Arrays.copyOf(b.nums, b.nums.length);return b;}public void display(){System.out.println("-----begin-----");System.out.println("int:" + num);System.out.println("Arrays:" + nums);System.out.println("nums[1]:" + nums[1]);System.out.println("-----end-----");}}public static void main(String [] args) throws CloneNotSupportedException{CloneTest ct = new CloneTest();//测试对象的属性为不可变属性。用浅拷贝即可满足要求。CloneTest.A a1 = ct.new A(1,"test1");CloneTest.A a2 = a1.clone();a2.num = 2;a2.str = "test2";a1.display();a2.display();//测试对象的属性为可变属性。注意对比试用浅拷贝和深拷贝的结果。int [] nums = {1,2,3};CloneTest.B b1 = ct.new B(1,nums);CloneTest.B b2 = b1.clone();b2.num = 2;b2.nums[1] = -2;b1.display();b2.display();}}



相关阅读:
Top