Object的clone方法

來源:互聯網
上載者:User
/*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.*/protected native Object clone() throws CloneNotSupportedException;

 

1 下面這些都沒有要求必須滿足:

            x.clone() != x                                         // 比如在子類的colne方法中return this;

            x.clone().getClass() == x.getClass()        // 子類實現clone方法,可以返沒有調用super.clone,而是new了一個子類對象,這時候就不是同一個類

            x.clone().equals(x)                                 // 子類實現colne方法中,如果修改了equals使用的欄位,顯然不相等

2 按照慣例,子類實現的clone方法應該會調用super.clone(),並且它的父類也都遵守這個約定,那麼x.clone().getClass() == x.getClass()  

3 慣例說,本身和複製應該相互獨立,完全兩碼事,x.clone().equals(x)應該返回false,要達到這個效果:需要在調用 super.clone()之後,修改複製對象包含的可變對象引用,這些可變對象需要深拷貝。

4 如果一個類只包含不可變對象和原生類型,那麼在調用super.clone()之後不需要修改欄位。

5  實現clone功能的CloneClass類實現了Cloneable介面,否則會拋出CloneNotSupportedException。

6 重載了clone()方法。最後在clone()方法中調用了super.clone(),這也意味著無論clone類的繼承結構是什麼樣的,super.clone()直接或間接調 用了java.lang.Object類的clone()方法。下面再詳細的解釋一下這幾點。 應該說第三點是最重要的,仔細觀察一下Object類的clone()一個native方法,native方法的效率一般來說都是遠高於java中的非 native方法。這也解釋了為什麼要用Object中clone()方法而不是先new一個類,然後把原始對象中的資訊賦到新對象中,雖然這也實現了
clone功能。對於第二點,也要觀察Object類中的clone()還是一個protected屬性的方法。這也意味著如果要應用clone()方法,必須繼承Object類,在Java中所有的類是預設繼承Object類的,也就不用關心這點了。

7 為了讓其它類能調用這個clone類的clone()方法,重載之後要把clone()方法的屬性設定為public。 、

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.