淺克隆、深克隆

// ----------1、直接復制對象 -----------


package ninthTestModel;

/*
 * 淺克隆
 * 深克隆
 * 序列化。
 * 區別在于對象有引用對象的時候 一個是指向同一個引用對象 一個是指向不同對象
 */

/*
 *      
 *   【淺克隆】,通常只是對克隆的實例進行復制,但里面的其他子對象,都是共用的。
   *   【深克隆】,克隆的時候會復制它的子對象的引用,里面所有的變量和子對象都是又額外拷貝了一份。
 * 
 */


// 周報
class WeekdayInformation{
    public String strInformation;   // 事件
    public String strTime;          // 時間
    public String strLocation;      // 地點
    
    public String getStrInformation() {
        return strInformation;
    }
    public String getStrTime() {
        return strTime;
    }
    public String getStrLocation() {
        return strLocation;
    }
    public void setStrInformation(String strInformation) {
        this.strInformation = strInformation;
    }
    public void setStrTime(String strTime) {
        this.strTime = strTime;
    }
    public void setStrLocation(String strLocation) {
        this.strLocation = strLocation;
    }
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        
        
        
        return "事件:"+strInformation+"\t時間:"+strTime+"\t地點:"+strLocation;
    }
}


public class CloneModel {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WeekdayInformation weekdayInformation=new WeekdayInformation();
        weekdayInformation.setStrInformation("吃飯");
        weekdayInformation.setStrTime("周一");
        weekdayInformation.setStrLocation("食堂");
        System.out.println(weekdayInformation.toString()); // ninthTestModel.WeekdayInformation@2a139a55
        
        WeekdayInformation week2=weekdayInformation;
        System.out.println(week2.toString());
        
        System.out.println("***********************");
        
        /*
         * weekdayInformation修改信息 則week2的信息也修改
         */
        System.out.println("weekdayInformation修改了信息:");
        weekdayInformation.setStrTime("周二");
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
        System.out.println("***********************");

        
        /*
         * week2修改信息 則weekdayInformation的信息也修改
         */
        System.out.println("week2修改了信息:");
        week2.setStrTime("周一");
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
    }
}

/*
結果如下:

事件:吃飯   時間:周一   地點:食堂
事件:吃飯   時間:周一   地點:食堂
***********************
weekdayInformation修改了信息:
事件:吃飯   時間:周二   地點:食堂
事件:吃飯   時間:周二   地點:食堂
***********************
week2修改了信息:
事件:吃飯   時間:周一   地點:食堂
事件:吃飯   時間:周一   地點:食堂

*/

// --------2、淺克隆 里面沒有引用對象---

package ninthTestModel;

/*
 * 淺克隆
 * 深克隆
 * 序列化。
 * 區別在于對象有引用對象的時候 一個是指向同一個引用對象 一個是指向不同對象
 */

/*
 *      
 *   【淺克隆】,通常只是對克隆的實例進行復制,但里面的其他子對象,都是共用的。
   *   【深克隆】,克隆的時候會復制它的子對象的引用,里面所有的變量和子對象都是又額外拷貝了一份。
 * 
 */

// 周報
class WeekdayInformation implements Cloneable{
    public String strInformation;   // 事件
    public String strTime;          // 時間
    public String strLocation;      // 地點
    
    public String getStrInformation() {
        return strInformation;
    }
    public String getStrTime() {
        return strTime;
    }
    public String getStrLocation() {
        return strLocation;
    }
    public void setStrInformation(String strInformation) {
        this.strInformation = strInformation;
    }
    public void setStrTime(String strTime) {
        this.strTime = strTime;
    }
    public void setStrLocation(String strLocation) {
        this.strLocation = strLocation;
    }
    
    // 淺克隆
    @Override
    protected WeekdayInformation clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
            Object object=null;
        
            object=super.clone();
            System.out.println("淺克隆");
            return (WeekdayInformation) object;
        
            // TODO Auto-generated catch block
        
            
        
        
        
        
    }
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "事件:"+strInformation+"\t時間:"+strTime+"\t地點:"+strLocation;
    }
}


public class CloneModel {

    public static void main(String[] args) throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        WeekdayInformation weekdayInformation=new WeekdayInformation();
        weekdayInformation.setStrInformation("吃飯");
        weekdayInformation.setStrTime("周一");
        weekdayInformation.setStrLocation("食堂");
        System.out.println(weekdayInformation.toString()); // ninthTestModel.WeekdayInformation@2a139a55
        
        WeekdayInformation week2=weekdayInformation.clone();
        System.out.println(week2.toString());
        
        System.out.println("***********************");
        
        /*
         * weekdayInformation修改信息 則week2的信息也修改
         */
        System.out.println("weekdayInformation修改了信息:");
        weekdayInformation.setStrTime("周二。");
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
        System.out.println("***********************");

        
        /*
         * week2修改信息 則weekdayInformation的信息也修改
         */
        System.out.println("week2修改了信息:");
        week2.setStrTime("周三。");
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
    }

}

結果如下:
/*
    事件:吃飯   時間:周一   地點:食堂
    淺克隆
    事件:吃飯   時間:周一   地點:食堂
    ***********************
    weekdayInformation修改了信息:
    事件:吃飯   時間:周二。  地點:食堂
    事件:吃飯   時間:周一   地點:食堂
    ***********************
    week2修改了信息:
    事件:吃飯   時間:周二。  地點:食堂
    事件:吃飯   時間:周三。  地點:食堂
*/
    
    
    

// ----------3、淺克隆 里面有被引用對象


package ninthTestModel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.Serializable;

import sixTestOop.TestEighthToString;

/*
 * 淺克隆
 * 深克隆
 * 序列化。
 * 區別在于對象有引用對象的時候 一個是指向同一個引用對象 一個是指向不同對象
 */

/*
 *      
 *   【淺克隆】,通常只是對克隆的實例進行復制,但里面的其他子對象,都是共用的。
   *   【深克隆】,克隆的時候會復制它的子對象的引用,里面所有的變量和子對象都是又額外拷貝了一份。
 *      注:!序列化問題 因為使用了序列化方法實現深克隆 因此類需要繼承Serializable 引用類也需要繼承
 */

class TestClone implements Serializable{
    public String testString;
    public void setTestString(String testString) {
        this.testString = testString;
    }
    public String getTestString() {
        return testString;
    }
    
}

// 周報
class WeekdayInformation implements Cloneable,Serializable{
    public String strInformation;   // 事件
    public String strTime;          // 時間
    public String strLocation;      // 地點
    public TestClone strClone;      // 被引用對象 
    
    
    public String getStrInformation() {
        return strInformation;
    }
    public String getStrTime() {
        return strTime;
    }
    public String getStrLocation() {
        return strLocation;
    }
    public TestClone getStrClone() {
        return strClone;
    }
    
    public void setStrInformation(String strInformation) {
        this.strInformation = strInformation;
    }
    public void setStrTime(String strTime) {
        this.strTime = strTime;
    }
    public void setStrLocation(String strLocation) {
        this.strLocation = strLocation;
    }
    public void setStrClone(TestClone strClone) {
        this.strClone = strClone;
    }
    
    // 淺克隆
    
    @Override
    protected WeekdayInformation clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        Object object=null;
        
        object=super.clone();
        System.out.println("淺克隆");
        return (WeekdayInformation) object;
        
    }
    
    
    
    // 深克隆
    public WeekdayInformation deepClone() throws IOException, ClassNotFoundException,OptionalDataException {
        System.out.println("******深克隆*********\n");
        
        WeekdayInformation weekdayInformation=null;
        
        // -- 將對象寫入流中
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bao);
        oos.writeObject(this);
        oos.close();
        
        // -- 將對象從流中取出
        ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois=new ObjectInputStream(bis);
        weekdayInformation=(WeekdayInformation) ois.readObject();
        ois.close();
        
        
        return weekdayInformation;
        
    }
    
    
    
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "事件:"+strInformation+"\t時間:"+strTime+"\t地點:"+strLocation+"\t測試:"+strClone.getTestString();
    }
}


public class CloneModel {

    public static void main(String[] args) throws CloneNotSupportedException, OptionalDataException, ClassNotFoundException, IOException {
        // TODO Auto-generated method stub
        WeekdayInformation weekdayInformation=new WeekdayInformation();
        TestClone testClone=new TestClone();
        testClone.setTestString("test");
        weekdayInformation.setStrInformation("吃飯");
        weekdayInformation.setStrTime("周一");
        weekdayInformation.setStrLocation("食堂");
        weekdayInformation.setStrClone(testClone);
        System.out.println(weekdayInformation.toString()); //如果沒重寫toString()方法顯示: ninthTestModel.WeekdayInformation@2a139a55
        
    
        WeekdayInformation  week2 = weekdayInformation.clone();
        
        System.out.println("克隆信息如下:"+week2+"\n");
        
        
        
        System.out.println("克隆對象是否相同:"+(weekdayInformation==week2));//
        
        //?? 為什么兩個都相同了?? 因為指向的同一個對象
        System.out.println("被引用對象TestClone是否相同:"+(weekdayInformation.getStrClone()==week2.getStrClone())); //相同? --相同
        System.out.println("基本對象String是否相同:"+(weekdayInformation.getStrInformation()==week2.getStrInformation())); //相同? -- 相同
        System.out.println("***********\n");
        
        System.out.println(week2.toString());
        
        System.out.println("***********************");
        
        /*
         * weekdayInformation修改信息 則week2的信息也修改
         */
        TestClone testClone1=new TestClone();
        testClone1.setTestString("test1");
        
        System.out.println("weekdayInformation修改了信息:");
        weekdayInformation.setStrTime("周二");
        weekdayInformation.setStrClone(testClone1); 
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
        System.out.println("***********************");

        
        /*
         * week2修改信息 則weekdayInformation的信息也修改
         */
        TestClone testClone2=new TestClone();
        testClone2.setTestString("test2");
        System.out.println("week2修改了信息:");
        week2.setStrTime("周三");
        week2.setStrClone(testClone2);  
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
    }

}
/*
輸出結果如下:

    事件:吃飯   時間:周一   地點:食堂   測試:test
    淺克隆
    克隆信息如下:事件:吃飯    時間:周一   地點:食堂   測試:test
    
    克隆對象是否相同:false
    被引用對象TestClone是否相同:true
    基本對象String是否相同:true
    ***********
    
    事件:吃飯   時間:周一   地點:食堂   測試:test
    ***********************
    weekdayInformation修改了信息:
    事件:吃飯   時間:周二   地點:食堂   測試:test1
    事件:吃飯   時間:周一   地點:食堂   測試:test
    ***********************
    week2修改了信息:
    事件:吃飯   時間:周二   地點:食堂   測試:test1
    事件:吃飯   時間:周三   地點:食堂   測試:test2
    

*/

// -----------4、深克隆 里面有被引用對象----------

package ninthTestModel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import sixTestOop.TestEighthToString;

/*
 * 淺克隆
 * 深克隆
 * 序列化。
 * 區別在于對象有引用對象的時候 一個是指向同一個引用對象 一個是指向不同對象
 */

/*
 *      
 *   【淺克隆】,通常只是對克隆的實例進行復制,但里面的其他子對象,都是共用的。
   *   【深克隆】,克隆的時候會復制它的子對象的引用,里面所有的變量和子對象都是又額外拷貝了一份。
 * 
 */

class TestClone{
    public String testString;
    public void setTestString(String testString) {
        this.testString = testString;
    }
    public String getTestString() {
        return testString;
    }
    
}

// 周報
class WeekdayInformation implements Cloneable{
    public String strInformation;   // 事件
    public String strTime;          // 時間
    public String strLocation;      // 地點
    public TestClone strClone;      // 被引用對象 
    
    
    public String getStrInformation() {
        return strInformation;
    }
    public String getStrTime() {
        return strTime;
    }
    public String getStrLocation() {
        return strLocation;
    }
    public TestClone getStrClone() {
        return strClone;
    }
    
    public void setStrInformation(String strInformation) {
        this.strInformation = strInformation;
    }
    public void setStrTime(String strTime) {
        this.strTime = strTime;
    }
    public void setStrLocation(String strLocation) {
        this.strLocation = strLocation;
    }
    public void setStrClone(TestClone strClone) {
        this.strClone = strClone;
    }
    
    // 淺克隆
    
    @Override
    protected WeekdayInformation clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        Object object=null;
        
        object=super.clone();
        System.out.println("淺克隆");
        return (WeekdayInformation) object;
        
    }
    
    
    
    // 深克隆
    public WeekdayInformation deepClone() throws IOException, ClassNotFoundException {
        // -- 將對象寫入流中
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bao);
        oos.writeObject(this);
        
        // -- 將對象從流中取出
        ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois=new ObjectInputStream(bis);
        
        return (WeekdayInformation) ois.readObject();
        
    
    }
    
    
    
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "事件:"+strInformation+"\t時間:"+strTime+"\t地點:"+strLocation+"\t測試:"+strClone.getTestString();
    }
}


public class CloneModel {

    public static void main(String[] args) throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        WeekdayInformation weekdayInformation=new WeekdayInformation();
        TestClone testClone=new TestClone();
        testClone.setTestString("test");
        weekdayInformation.setStrInformation("吃飯");
        weekdayInformation.setStrTime("周一");
        weekdayInformation.setStrLocation("食堂");
        weekdayInformation.setStrClone(testClone);
        System.out.println(weekdayInformation.toString()); // 如果沒有重寫toString()方法會顯示:ninthTestModel.WeekdayInformation@2a139a55
        
        WeekdayInformation week2=weekdayInformation.deepClone();// 這里調用深克隆實現
        System.out.println("被引用對象是否相同:"+(weekdayInformation.getStrClone()==week2.getStrClone())); //相同? 不同 
        System.out.println("是否相同:"+(weekdayInformation.getStrInformation()==week2.getStrInformation())); //相同? 不同
        
        System.out.println(week2.toString());
        
        System.out.println("***********************");
        
        /*
         * weekdayInformation修改信息 則week2的信息也修改
         */
        TestClone testClone1=new TestClone();
        testClone1.setTestString("test1");
        
        System.out.println("weekdayInformation修改了信息:");
        weekdayInformation.setStrTime("周二");
        weekdayInformation.setStrClone(testClone1); 
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
        
        System.out.println("***********************");

        
        /*
         * week2修改信息 則weekdayInformation的信息也修改
         */
        TestClone testClone2=new TestClone();
        testClone2.setTestString("test2");
        System.out.println("week2修改了信息:");
        week2.setStrTime("周三");
        week2.setStrClone(testClone2);  
        System.out.println(weekdayInformation.toString());
        System.out.println(week2.toString());
        
    }

}
/*      
輸出結果如下:

        事件:吃飯   時間:周一   地點:食堂   測試:test
        淺克隆
        克隆信息如下:事件:吃飯    時間:周一   地點:食堂   測試:test
        
        克隆對象是否相同:false
        被引用對象TestClone是否相同:true
        基本對象String是否相同:true
        ***********
        
        事件:吃飯   時間:周一   地點:食堂   測試:test
        ***********************
        weekdayInformation修改了信息:
        事件:吃飯   時間:周二   地點:食堂   測試:test1
        事件:吃飯   時間:周一   地點:食堂   測試:test
        ***********************
        week2修改了信息:
        事件:吃飯   時間:周二   地點:食堂   測試:test1
        事件:吃飯   時間:周三   地點:食堂   測試:test2

*/
    


    

自我理解:

對象一 對象二=對象一的復制/淺克隆/深克隆

復制:對象一=對象二 二者完全值得同一個 如果對象一修改 對象二則跟著修改

淺克隆:對象一、二不同,但是對于被引用對象指向的會是同一個對象,

深克隆:對象一、二不同,是完全的創建出了一個新的對象,
對于被引用對象也不同,如這里我所用到的引用對象testClone。

問題:

但是有個問題我沒搞懂,淺克隆的時候為什么String類型的對象也指向的同一個?

也就是在例子三中

被引用對象TestClone是否相同:true //--這個應該是true

基本對象String是否相同:true //-- 這個不應該是false嘛?怎么回事true

源碼:

System.out.println("被引用對象TestClone是否相同:"+(weekdayInformation.getStrClone()==week2.getStrClone())); //相同?
System.out.println("基本對象String是否相同:"+(weekdayInformation.getStrInformation()==week2.getStrInformation())); //相同?

-- 以上如果有錯,歡迎指出。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內容

  • 說克隆之前,首先要說一下,js的數據類型 js中的數據類型分為兩大類:原始類型和對象類型。 1)原始類型包括:數值...
    你期待的花開閱讀 280評論 1 4
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,836評論 18 139
  • 307、setValue:forKey和setObject:forKey的區別是什么? 答:1, setObjec...
    AlanGe閱讀 1,575評論 0 1
  • 1大同小異的工作周報 Sunny軟件公司一直使用自行開發的一套OA (Office Automatic,辦公自動化...
    justCode_閱讀 1,177評論 0 3
  • Java 深克隆(DeepClone)與淺克隆(ShallowClone)是原型設計模式的靈魂。記錄結構:--什么...
    markfork閱讀 1,031評論 0 3