Part I
沒啥好說的,直接開始Part II吧。
Part II
談到了對象的克隆,就不得不說為什么要對對象進行克隆。Java中所有的對象都是保存在堆中,而堆是供全局共享的。也就是說,如果同一個Java程序的不同方法,只要能拿到某個對象的引用,引用者就可以隨意的修改對象的內部數據(前提是這個對象的內部數據通過get/set方法曝露出來)。有的時候,我們編寫的代碼想讓調用者只獲得該對象的一個拷貝(也就是一個內容完全相同的對象,但是在內存中存在兩個這樣的對象),有什么辦法可以做到呢?當然是克隆咯。
Part III
首先,我們是程序員,當然是用我們程序員的語言來交流。
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的實現(可用eclipse自動生成)
}@Overridepublic boolean equals(Object obj) {// 省略equals的實現(可用eclipse自動生成)
}// 省略一大堆get/set方法
}
上述代碼構建了一個User類,并且實現了java.lang.Cloneable接口。顧名思義,Cloneable的意思就是說明這個類可以被克隆的意思。
而我們先去看看java.lang.Cloneable這個接口有些什么。
/*
* @(#)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 theCloneable
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.*
* 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 {@link java.lang.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.** @author unascribed* @version 1.17, 11/17/05* @see java.lang.CloneNotSupportedException* @see java.lang.Object#clone()* @since JDK1.0*/public interface Cloneable {}
不要驚訝,沒錯,除了一大堆的雞腸以外,這個接口沒有定義任何的方法簽名。也就是說,我們要克隆一個對象,但是他又不給我提供一個方法。那該怎么辦呢?不怕,我們還有全能的Object類,別忘記他可是所有類的始祖?。ㄉ褚话愕拇嬖谥?,所以,有事沒事都該去問候一下他老人家。
/*
* @(#)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;
/**
* ClassObject
is the root of the class hierarchy.* Every class hasObject
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 x, the expression:*** x.clone() != x* will 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.** @return a clone of this instance.* @exception CloneNotSupportedException if the object's class does not* support theCloneable
interface. Subclasses* that override theclone
method can also* throw this exception to indicate that an instance cannot* be cloned.* @see java.lang.Cloneable*/protected native Object clone() throws CloneNotSupportedException;}
呵呵,又是一大串的雞腸,別以為我是來湊字數的,這些都是Sun公司Java開發(fā)人員寫的技術文章,多看看少說話吧。
沒錯,又是個native方法,果然是個高深的東西,不過我們還是要占一下他的便宜。而且他這個方法是protected的,分明就是叫我們去占便宜的。
再繼續(xù)看看下面測試代碼。
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); // true
System.out.println(u1.equals(u2)); // true
System.out.println(u1 == u3); // false
System.out.println(u1.equals(u3)); // true
}}
這個clone()方法果然牛,一下子就把我們的對象克隆了一份,執(zhí)行結果也符合我們的預期,u1和u3的地址不同但是內容相同。
Part IV
通過上述的例子,我們可以看出,要讓一個對象進行克隆,其實就是兩個步驟:
1. 讓該類實現java.lang.Cloneable接口;
2. 重寫(override)Object類的clone()方法。
但是,事實上真的是如此簡單嗎?再看下面的代碼。
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ī)矩
}
上面定義了一個Administrator類,這個類持有一個User類的對象。接下來我們看看對Administrator對象進行克隆會有什么效果。
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); // true
System.out.println(a1.equals(a2)); // true
System.out.println(a1 == a3); // false
System.out.println(a1.equals(a3)); // true
System.out.println(a1.getUser() == a3.getUser()); //true ! It's not our expected!!!!!
System.out.println(a1.getUser().equals(a3.getUser())); //true
}}
呵呵呵!出問題了吧。Java哪是那么容易就能駕馭的說!
這里我們就可以引入兩個專業(yè)的術語:淺克?。╯hallow clone)和深克?。╠eep clone)。
所謂的淺克隆,顧名思義就是很表面的很表層的克隆,如果我們要克隆Administrator對象,只克隆他自身以及他包含的所有對象的引用地址。
而深克隆,就是非淺克隆。克隆除自身以外所有的對象,包括自身所包含的所有對象實例。至于深克隆的層次,由具體的需求決定,也有“N層克隆”一說。
但是,所有的基本(primitive)類型數據,無論是淺克隆還是深克隆,都會進行原值克隆。畢竟他們都不是對象,不是存儲在堆中。注意:基本數據類型并不包括他們對應的包裝類。
如果我們想讓對象進行深度克隆,我們可以這樣修改Administrator類。
@Overrideprotected Object clone() throws CloneNotSupportedException {Administrator admin = (Administrator) super.clone();
admin.user = (User) admin.user.clone();return admin;
}
由于Boolean會對值進行緩存處理,所以我們沒必要對Boolean的對象進行克隆。并且Boolean類也沒有實現java.lang.Cloneable接口。
Part V
1. 讓該類實現java.lang.Cloneable接口;
2. 確認持有的對象是否實現java.lang.Cloneable接口并提供clone()方法;
3. 重寫(override)Object類的clone()方法,并且在方法內部調用持有對象的clone()方法;
4. ……
5. 多麻煩啊,調來調去的,如果有N多個持有的對象,那就要寫N多的方法,突然改變了類的結構,還要重新修改clone()方法。
難道就沒有更好的辦法嗎?
Part VI
接下來要重點介紹一下使用java.lang.Serializable來實現對象的深度克隆。
首先,我們編寫一個工具類并提供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")
public static T cloneTo(T src) throws RuntimeException {ByteArrayOutputStream memoryBuffer = new ByteArrayOutputStream();
ObjectOutputStream out = null;
ObjectInputStream in = null;
T dist = null;
try {
out = new ObjectOutputStream(memoryBuffer);
out.writeObject(src);out.flush();in = new ObjectInputStream(new ByteArrayInputStream(memoryBuffer.toByteArray()));dist = (T) in.readObject();} catch (Exception e) {
throw new RuntimeException(e);} finally {
if (out != null)try {
out.close();out = null;
} catch (IOException e) {
throw new RuntimeException(e);}if (in != null)try {
in.close();in = null;
} catch (IOException e) {
throw new RuntimeException(e);}}return dist;
}}
看不懂,沒關系,直接拿去用就可以了。嘻嘻。
接下來我們測試一下是否能通過這個工具來實現深度克隆。
又是這個可愛的TestCase,可憐的每次都要動他……
import java.util.Date;
import org.junit.Test;
public class TestCase {@Testpublic void testCloneTo() {Administrator src = new Administrator(new User("Kent", "123456", new Date()), true);Administrator dist = BeanUtil.cloneTo(src);System.out.println(src == dist); // false
System.out.println(src.equals(dist)); // true
System.out.println(src.getUser() == dist.getUser()); //false ! Well done!
System.out.println(src.getUser().equals(dist.getUser())); //true
}}
好了,無論你的對象有多么的復雜,只要這些對象都能夠實現java.lang.Serializable接口,就可以進行克隆,而且這種克隆的機制是JVM完成的,不需要修改實體類的代碼,方便多了。
為什么這么簡單就可以實現對象的克隆呢?java.lang.Serializable接口又是干嘛用的呢?如果想知道這些問題的答案,請匯款到0123 4567 8901 2345678。
……
開玩笑咔……這些問題的答案,我都留在了java.lang.Serializable的專題里面,請各位翻一下我的日志吧。PS.有可能這幾天之內會出來,看哪天無聊寫寫吧……先睡夠再說……