Part II<br>
談到了物件的克隆,就不得不說為什麼要對物件進行克隆。 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;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public int hashCode() {
// 省略equals的实现(可用eclipse自动生成)
}
@Override
public 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 the Cloneable 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.
*
任何的方法簽名。也就是說,我們要複製一個對象,但他又不提供我一個方法。那該怎麼辦呢?不怕,我們還有全能的Object類,別忘記他可是所有類的始祖啊(神一般的存在著),所以,有事沒事都該去問候一下他老人家。 #erre
* 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.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 Object is the root of the class hierarchy.
* Every class has Object 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 {
<br>
// 省略N多的代码rrree湊字數的,這些都是Sun公司Java開發人員寫的技術文章,多看看少說話。
沒錯,又是個native方法,果然是個高深的東西,不過我們還是要佔一下他的便宜。而且他這個方法是protected的,分明就是叫我們去佔便宜的。
再繼續看看下面測試程式碼。
/**
* 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.
*
這個clone()方法果然牛,一下子就把我們的物件克隆了一份,執行結果也符合我們的預期,u1和u3的地址不同但是內容相同。
Part IV
透過上述的例子,我們可以看出,要讓一個物件克隆,其實就是兩個步驟:
1. 讓該類別實作java.lang.Cloneable介面;
2. 重寫(override)Object類別的clone()方法。 但是,事實上真的是如此簡單嗎?再看下面的程式碼。
* 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
上面定義了一個Administrator類,這個類別持有一個User類別的物件。接下來我們來看看Administrator物件進行克隆會有什麼效果。
* 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 the Cloneable interface. Subclasses
* that override the clone method can also
* throw this exception to indicate that an instance cannot
* be cloned.
* @see java.lang.Cloneable
*/
protected native Object clone() throws CloneNotSupportedException;
呵呵呵!出問題了吧。 Java哪是那麼容易駕馭的說!
這裡我們就可以引入兩個專業的術語:淺克隆(shallow clone)和深克隆(deep clone)。
所謂的淺克隆,顧名思義就是很表面的很表層的克隆,如果我們要克隆Administrator對象,只克隆他自身以及他包含的所有對象的
引用地址。
而深克隆,就是非淺克隆。複製除自身以外所有的對象,包括自身所包含的所有
對象實例。至於深克隆的層次,由具體的需求決定,也有「N層克隆」一說。
但是,所有的基本(primitive)類型數據,無論是淺克隆還是深克隆,都會進行原值克隆。畢竟他們都不是對象,不是儲存在堆中。注意:基本資料類型並不包括他們對應的包裝類別。 如果我們想讓物件進行深度克隆,我們可以這樣修改Administrator類別。
}
import java.util.Date;
import org.junit.Test;
public class TestCase {
<br>
@Test
由於Boolean會對值進行快取處理,所以我們沒必要將Boolean的物件複製。且Boolean類別也沒有實作java.lang.Cloneable介面。
Part V1. 让该类实现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 {
<br>
@Test
public void testCloneTo() {
Administrator src = new Administrator(new User("Kent", "123456", new Date()), true);
Administrator dist = BeanUtil.cloneTo(src);
<br>
System.out.println(src == dist); // false
System.out.println(src.equals(dist)); // true
<br>
System.out.println(src.getUser() == dist.getUser()); //false ! Well done!
System.out.println(src.getUser().equals(dist.getUser())); //true
}
<br>
}
好了,无论你的对象有多么的复杂,只要这些对象都能够实现java.lang.Serializable接口,就可以进行克隆,而且这种克隆的机制是JVM完成的,不需要修改实体类的代码,方便多了。
以上是Java中物件複製(Clone)的實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!