)Java對象複製(Clone)及Cloneable介面、Serializable介面的深入探討,phpclone對象複製

來源:互聯網
上載者:User

(轉)Java對象複製(Clone)及Cloneable介面、Serializable介面的深入探討,phpclone對象複製

原文地址:http://blog.csdn.net/kenthong/article/details/5758884

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;
}
@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.java1.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.
 *

 

 * 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.java1.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 {
    
   // 省略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 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;
}

呵呵,又是一大串的雞腸,別以為我是來湊字數的,這些都是Sun公司Java開發人員寫的技術文章,多看看少說話吧。

沒錯,又是個native方法,果然是個高深的東西,不過我們還是要佔一下他的便宜。而且他這個方法是protected的,分明就是叫我們去佔便宜的。

再繼續看看下面測試代碼。

import java.util.Date;
import org.junit.Test;
public class TestCase {

@Test
public 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()方法果然牛,一下子就把我們的對象複製了一份,執行結果也符合我們的預期,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;
}

@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public int hashCode() {
// 老規矩
}
@Override
public boolean equals(Object obj) {
// 老規矩
}
// 老規矩
}

上面定義了一個Administrator類,這個類持有一個User類的對象。接下來我們看看對Administrator對象進行複製會有什麼效果。

import java.util.Date;
import org.junit.Test;
public class TestCase {
@Test
public 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哪是那麼容易就能駕馭的說!

這裡我們就可以引入兩個專業的術語:淺複製(shallow clone)和深複製(deep clone)。

所謂的淺複製,顧名思義就是很表面的很表層的複製,如果我們要複製Administrator對象,只複製他自身以及他包含的所有對象的引用地址。

而深複製,就是非淺複製。複製除自身以外所有的對象,包括自身所包含的所有對象執行個體。至於深複製的層次,由具體的需求決定,也有“N層複製”一說。

但是,所有的基本(primitive)類型資料,無論是淺複製還是深複製,都會進行原值複製。畢竟他們都不是對象,不是儲存在堆中。注意:基礎資料型別 (Elementary Data Type)並不包括他們對應的封裝類。

如果我們想讓對象進行深度複製,我們可以這樣修改Administrator類。

@Override
protected 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 {

@Test
public 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.有可能這幾天之內會出來,看哪天無聊寫寫吧……先睡夠再說……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.