日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

j详细说明ava于clone办法

發(fā)布時(shí)間:2025/4/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 j详细说明ava于clone办法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址:http://leihuang.org/2014/11/14/java-clone/


In java, it essentially means the ability to create an object with similar state as the original object.

什么是clone

字典中的意思就是復(fù)制(強(qiáng)調(diào)跟原來(lái)的一模一樣)。

*By default, java cloning is ‘field by field copy’ *.由于Object類不知道詳細(xì)類的結(jié)構(gòu),無(wú)法確定哪個(gè)clone方法將被調(diào)用。

所以JVM規(guī)定clone,做例如以下操作。

  • 假設(shè)這個(gè)類僅僅有原始數(shù)據(jù)類型成員的話。那么須要?jiǎng)?chuàng)建一個(gè)一模一樣的對(duì)象,而且返回這個(gè)新對(duì)象的引用。
  • 假設(shè)這個(gè)類包括了不論什么類類型成員的話,那么僅僅有這些成員的對(duì)象引用被復(fù)制。且原始對(duì)象和克隆對(duì)象的成員的引用將指向同一個(gè)對(duì)象。
  • 翻譯的不好。%>_<%

  • If the class has only primitive data type members then a completely new copy of the object will be created and the reference to the new object copy will be returned.
  • If the class contains members of any class type then only the object references to those members are copied and hence the member references in both the original object as well as the cloned object refer to the same object.
  • 除了上面兩條默認(rèn)的操作之外。你也能夠重載clone方法,制定一套自己的clone方法。


    java中clone規(guī)則

    每種支持clone的語(yǔ)言都有它自己的規(guī)則,java也不例外。java中假設(shè)一個(gè)類需要支持clone的話。必需要做例如以下事情:

  • 你必須繼承Cloneable接口---Cloneable interface is broken in java。otherwise throws java.lang.CloneNotSupportedException.
  • 必須重寫(xiě)Object類中的clone()方法
  • /* 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: 1) x.clone() != x will be true 2) x.clone().getClass() == x.getClass() will be true, but these are not absolute requirements. 3) x.clone().equals(x) will be true, this is not an absolute requirement. */ protected native Object [More ...] clone() throws CloneNotSupportedException;

  • 第一條表明,clone對(duì)象分配有獨(dú)立的內(nèi)存地址。
  • 第二條表明,原始的和克隆的對(duì)象應(yīng)該具有同樣類型,但這不是必須的
  • 第三條表明。原始的和克隆的對(duì)象調(diào)用equals方法的話,應(yīng)該時(shí)相等的。但這不是必須的。
  • 以下我們通過(guò)樣例來(lái)分析:

    class Father implements Cloneable{private int age ;private String name ;private Son son ;public Father(int age,String name,Son son){this.age = age ;this.name = name ;this.son = son ;}public Son getSon(){return this.son ;}@Overrideprotected Object clone()throws CloneNotSupportedException{return super.clone() ;} } class Son{private int age ;private String name ;public Son(int age ,String name){this.age = age ;this.name = name ;}public void setName(String name){this.name = name ;}public String getName(){return this.name ;} } public class CloneDemo {public static void main(String args[]) throws CloneNotSupportedException{Son s = new Son(10,"Jack") ;Father fa = new Father(40,"Tom",s) ;Father clonedFa = (Father) fa.clone() ;System.out.println(fa!=clonedFa);System.out.println(clonedFa.getClass()==fa.getClass());System.out.println(clonedFa.equals(fa));//now we change the fa's son name by the clonedFa's son nameclonedFa.getSon().setName("Jay");System.out.println(fa.getSon().getName());} } /*print: true true false Jay */

    上面的代碼中能夠看出。原始的和克隆的Father類對(duì)象。擁有指向同一對(duì)象的兩個(gè)引用。所以能夠通過(guò)改變clonedFa中的Son來(lái)改變fa中Son對(duì)象。這就時(shí)所謂的淺拷貝.以下我們具體討論一下淺拷貝和深拷貝。

    淺拷貝(Shallow Cloning)

    這是java中默認(rèn)的實(shí)現(xiàn)。上面的樣例中就是淺拷貝。不創(chuàng)建一個(gè)新的克隆拷貝對(duì)象Son,而是直接兩個(gè)引用指向同一對(duì)象。

    深拷貝

    If we want a clone which is independent of original and making changes in clone should not affect original.you can try Deep Cloning.

    we change the clone() method in the Father class .

    @Override protected Object clone() throws CloneNotSupportedException {Father fa = (Father)super.clone(); fa.setSon((Son)fa.getSon().clone());return fa; }

    and we need Override the clone() method in the Son class. like this.

    @Override protected Object clone() throws CloneNotSupportedException {return super.clone(); }

    如今我們已經(jīng)實(shí)現(xiàn)深拷貝了。

    拷貝構(gòu)造函數(shù)

    拷貝構(gòu)造函數(shù)時(shí)一種特殊的構(gòu)造器。它講自己的類類型作為參數(shù)。我們傳遞一個(gè)類的實(shí)例給拷貝構(gòu)造函數(shù)。然后它將返回一個(gè)新的類實(shí)例。lets see this in example

    public class PointOne {private Integer x;private Integer y;public PointOne(PointOne point){this.x = point.x;this.y = point.y;} }

    假設(shè)要繼承它的話,則須要復(fù)制子類的參數(shù),和傳遞參數(shù)給父類的構(gòu)造器。

    例如以下:

    public class PointTwo extends PointOne{private Integer z;public PointTwo(PointTwo point){super(point); //Call Super class constructor herethis.z = point.z;} }

    以下是測(cè)試代碼:

    class Test {public static void main(String[] args){PointOne one = new PointOne(1,2);PointTwo two = new PointTwo(1,2,3);PointOne clone1 = new PointOne(one);PointOne clone2 = new PointOne(two);//Let check for class typesSystem.out.println(clone1.getClass());System.out.println(clone2.getClass());} } Output: class corejava.cloning.PointOne class corejava.cloning.PointOne

    你也能夠使用靜態(tài)工廠的方法來(lái)實(shí)現(xiàn)它。

    public class PointOne {private Integer x;private Integer y;public PointOne(Integer x, Integer y){this.x = x;this.y = y;}public PointOne copyPoint(PointOne point) throws CloneNotSupportedException{if(!(point instanceof Cloneable)){throw new CloneNotSupportedException("Invalid cloning");}//Can do multiple other things herereturn new PointOne(point.x, point.y);} }

    Cloning with serialization

    這是例外一種深拷貝的方法。這里就不多講了,具體見(jiàn):A mini guide for implementing serializable interface in java

    best practices

    1) When you don’t know whether you can call the clone() method of a particular class as you are not sure if it is implemented in that class, you can check with checking if the class is instance of “Cloneable” interface as below.

    if(obj1 instanceof Cloneable){obj2 = obj1.clone(); } //Dont do this. Cloneabe dont have any methods obj2 = (Cloneable)obj1.clone();

    2) No constructor is called on the object being cloned. As a result, it is your responsibility, to make sure all the members have been properly set. Also, if you are keeping track of number of objects in system by counting the invocation of constructors, you got a new additional place to increment the counter.

    Reference:

  • A guide to object cloning in java
  • Effective--Java Item 11: Override clone judiciously(講的更具體,各種clone方式的優(yōu)缺點(diǎn)都講了)

  • 2014-11-14 15:48:12

    Brave,Happy,Thanksgiving !


    版權(quán)聲明:本文博主原創(chuàng)文章。博客,未經(jīng)同意不得轉(zhuǎn)載。

    總結(jié)

    以上是生活随笔為你收集整理的j详细说明ava于clone办法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。