轉(zhuǎn)載地址:http://kentkwan.iteye.com/blog/739514
Part I
沒(méi)啥好說(shuō)的,直接開(kāi)始Part II吧。
Part II
談到了對(duì)象的克隆,就不得不說(shuō)為什么要對(duì)對(duì)象進(jìn)行克隆。java中所有的對(duì)象都是保存在堆中,而堆是供全局共享的。也就是說(shuō),如果同一個(gè)Java程序的不同方法,只要能拿到某個(gè)對(duì)象的引用,引用者就可以隨意的修改對(duì)象的內(nèi)部數(shù)據(jù)(前提是這個(gè)對(duì)象的內(nèi)部數(shù)據(jù)通過(guò)get/set方法曝露出來(lái))。有的時(shí)候,我們編寫(xiě)的代碼想讓調(diào)用者只獲得該對(duì)象的一個(gè)拷貝(也就是一個(gè)內(nèi)容完全相同的對(duì)象,但是在內(nèi)存中存在兩個(gè)這樣的對(duì)象),有什么辦法可以做到呢?當(dāng)然是克隆咯。
Part III
首先,我們是程序員,當(dāng)然是用我們程序員的語(yǔ)言來(lái)交流。
import java.util.Date;public class User implements Cloneable {PRivate String username;private String passWord;private Date birthdate;public User(String username, String password, Date birthdate) {this.username = username;this.password = password;this.birthdate = birthdate;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}@Overridepublic int hashCode() {// 省略equals的實(shí)現(xiàn)(可用eclipse自動(dòng)生成)}@Overridepublic boolean equals(Object obj) {// 省略equals的實(shí)現(xiàn)(可用eclipse自動(dòng)生成)}// 省略一大堆get/set方法}上述代碼構(gòu)建了一個(gè)User類(lèi),并且實(shí)現(xiàn)了java.lang.Cloneable接口。顧名思義,Cloneable的意思就是說(shuō)明這個(gè)類(lèi)可以被克隆的意思。
而我們先去看看java.lang.Cloneable這個(gè)接口有些什么。
/** @(#)Cloneable.java 1.17 05/11/17** Copyright 2006 Sun Microsystems, Inc. All rights reserved.* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*/package java.lang;/*** A class implements the <code>Cloneable</code> interface to* indicate to the {@link java.lang.Object#clone()} method that it* is legal for that method to make a* field-for-field copy of instances of that class.* <p>* Invoking Object's clone method on an instance that does not implement the* <code>Cloneable</code> interface results in the exception* <code>CloneNotSupportedException</code> being thrown.* <p>* By convention, classes that implement this interface should override* <tt>Object.clone</tt> (which is protected) with a public method.* See {@link java.lang.Object#clone()} for details on overriding this* method.* <p>* Note that this interface does <i>not</i> contain the <tt>clone</tt> 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.** @author unascribed* @version 1.17, 11/17/05* @see java.lang.CloneNotSupportedException* @see java.lang.Object#clone()* @since JDK1.0*/public interface Cloneable {}不要驚訝,沒(méi)錯(cuò),除了一大堆的雞腸以外,這個(gè)接口沒(méi)有定義任何的方法簽名。也就是說(shuō),我們要克隆一個(gè)對(duì)象,但是他又不給我提供一個(gè)方法。那該怎么辦呢?不怕,我們還有全能的Object類(lèi),別忘記他可是所有類(lèi)的始祖啊(神一般的存在著),所以,有事沒(méi)事都該去問(wèn)候一下他老人家。
/** @(#)Object.java 1.73 06/03/30** Copyright 2006 Sun Microsystems, Inc. All rights reserved.* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*/package java.lang;/*** Class <code>Object</code> is the root of the class hierarchy.* Every class has <code>Object</code> as a superclass. All objects,* including arrays, implement the methods of this class.** @author unascribed* @version 1.73, 03/30/06* @see java.lang.Class* @since JDK1.0*/public class Object {// 省略N多的代碼/*** 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 <tt>x</tt>, the expression:* <blockquote>* <pre>* x.clone() != x</pre></blockquote>* will be true, and that the expression:* <blockquote>* <pre>* x.clone().getClass() == x.getClass()</pre></blockquote>* will be <tt>true</tt>, but these are not absolute requirements.* While it is typically the case that:* <blockquote>* <pre>* x.clone().equals(x)</pre></blockquote>* will be <tt>true</tt>, this is not an absolute requirement.* <p>* By convention, the returned object should be obtained by calling* <tt>super.clone</tt>. If a class and all of its superclasses (except* <tt>Object</tt>) obey this convention, it will be the case that* <tt>x.clone().getClass() == x.getClass()</tt>.* <p>* 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 <tt>super.clone</tt> 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 <tt>super.clone</tt>* need to be modified.* <p>* The method <tt>clone</tt> for class <tt>Object</tt> performs a* specific cloning Operation. First, if the class of this object does* not implement the interface <tt>Cloneable</tt>, then a* <tt>CloneNotSupportedException</tt> is thrown. Note that all arrays* are considered to implement the interface <tt>Cloneable</tt>.* 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.* <p>* The class <tt>Object</tt> does not itself implement the interface* <tt>Cloneable</tt>, so calling the <tt>clone</tt> method on an object* whose class is <tt>Object</tt> will result in throwing an* exception at run time.** @return a clone of this instance.* @exception CloneNotSupportedException if the object's class does not* support the <code>Cloneable</code> interface. Subclasses* that override the <code>clone</code> method can also* throw this exception to indicate that an instance cannot* be cloned.* @see java.lang.Cloneable*/protected native Object clone() throws CloneNotSupportedException;}呵呵,又是一大串的雞腸,別以為我是來(lái)湊字?jǐn)?shù)的,這些都是Sun公司Java開(kāi)發(fā)人員寫(xiě)的技術(shù)文章,多看看少說(shuō)話(huà)吧。
沒(méi)錯(cuò),又是個(gè)native方法,果然是個(gè)高深的東西,不過(guò)我們還是要占一下他的便宜。而且他這個(gè)方法是protected的,分明就是叫我們?nèi)フ急阋说摹?/p>
再繼續(xù)看看下面測(cè)試代碼。
import java.util.Date;import org.junit.Test;public class TestCase {@Testpublic void testUserClone() throws CloneNotSupportedException {User u1 = new User("Kent", "123456", new Date());User u2 = u1;User u3 = (User) u1.clone();System.out.println(u1 == u2); // trueSystem.out.println(u1.equals(u2)); // trueSystem.out.println(u1 == u3); // falseSystem.out.println(u1.equals(u3)); // true}}這個(gè)clone()方法果然牛,一下子就把我們的對(duì)象克隆了一份,執(zhí)行結(jié)果也符合我們的預(yù)期,u1和u3的地址不同但是內(nèi)容相同。
Part IV
通過(guò)上述的例子,我們可以看出,要讓一個(gè)對(duì)象進(jìn)行克隆,其實(shí)就是兩個(gè)步驟:
1. 讓該類(lèi)實(shí)現(xiàn)java.lang.Cloneable接口;
2. 重寫(xiě)(override)Object類(lèi)的clone()方法。
但是,事實(shí)上真的是如此簡(jiǎn)單嗎?再看下面的代碼。
public class Administrator implements Cloneable {private User user;private Boolean editable;public Administrator(User user, Boolean editable) {this.user = user;this.editable = editable;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}@Overridepublic int hashCode() {// 老規(guī)矩}@Overridepublic boolean equals(Object obj) {// 老規(guī)矩}// 老規(guī)矩}上面定義了一個(gè)Administrator類(lèi),這個(gè)類(lèi)持有一個(gè)User類(lèi)的對(duì)象。接下來(lái)我們看看對(duì)Administrator對(duì)象進(jìn)行克隆會(huì)有什么效果。
import java.util.Date;import org.junit.Test;public class TestCase {@Testpublic void testAdministratorClone() throws CloneNotSupportedException {Administrator a1 = new Administrator(new User("Kent", "123456", new Date()), true);Administrator a2 = a1;Administrator a3 = (Administrator) a1.clone();System.out.println(a1 == a2); // trueSystem.out.println(a1.equals(a2)); // trueSystem.out.println(a1 == a3); // falseSystem.out.println(a1.equals(a3)); // trueSystem.out.println(a1.getUser() == a3.getUser()); //true ! It's not our expected!!!!!System.out.println(a1.getUser().equals(a3.getUser())); //true}}呵呵呵!出問(wèn)題了吧。Java哪是那么容易就能駕馭的說(shuō)!
這里我們就可以引入兩個(gè)專(zhuān)業(yè)的術(shù)語(yǔ):淺克隆(shallow clone)和深克隆(deep clone)。
所謂的淺克隆,顧名思義就是很表面的很表層的克隆,如果我們要克隆Administrator對(duì)象,只克隆他自身以及他包含的所有對(duì)象的引用地址。
而深克隆,就是非淺克隆。克隆除自身以外所有的對(duì)象,包括自身所包含的所有對(duì)象實(shí)例。至于深克隆的層次,由具體的需求決定,也有“N層克隆”一說(shuō)。
但是,所有的基本(primitive)類(lèi)型數(shù)據(jù),無(wú)論是淺克隆還是深克隆,都會(huì)進(jìn)行原值克隆。畢竟他們都不是對(duì)象,不是存儲(chǔ)在堆中。注意:基本數(shù)據(jù)類(lèi)型并不包括他們對(duì)應(yīng)的包裝類(lèi)。
如果我們想讓對(duì)象進(jìn)行深度克隆,我們可以這樣修改Administrator類(lèi)。
@Overrideprotected Object clone() throws CloneNotSupportedException {Administrator admin = (Administrator) super.clone();admin.user = (User) admin.user.clone();return admin;}由于Boolean會(huì)對(duì)值進(jìn)行緩存處理,所以我們沒(méi)必要對(duì)Boolean的對(duì)象進(jìn)行克隆。并且Boolean類(lèi)也沒(méi)有實(shí)現(xiàn)java.lang.Cloneable接口。
Part V
1. 讓該類(lèi)實(shí)現(xiàn)java.lang.Cloneable接口;
2. 確認(rèn)持有的對(duì)象是否實(shí)現(xiàn)java.lang.Cloneable接口并提供clone()方法;
3. 重寫(xiě)(override)Object類(lèi)的clone()方法,并且在方法內(nèi)部調(diào)用持有對(duì)象的clone()方法;
4. ……
5. 多麻煩啊,調(diào)來(lái)調(diào)去的,如果有N多個(gè)持有的對(duì)象,那就要寫(xiě)N多的方法,突然改變了類(lèi)的結(jié)構(gòu),還要重新修改clone()方法。
難道就沒(méi)有更好的辦法嗎?
Part VI
接下來(lái)要重點(diǎn)介紹一下使用java.lang.Serializable來(lái)實(shí)現(xiàn)對(duì)象的深度克隆。
首先,我們編寫(xiě)一個(gè)工具類(lèi)并提供cloneTo()方法。
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public abstract class BeanUtil {@SuppressWarnings("unchecked")