解析JAVA深度複製與淺度複製的區別詳解_java

來源:互聯網
上載者:User
在JAVA複製對象不能簡單的使用clone方法,clone方法只是進行淺複製。請看下方:
深度複製類:
Java代碼
複製代碼 代碼如下:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class DepthClone {
    public  final static Object objectCopy(Object oldObj) {
        Object newObj = null;
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(oldObj);//來源物件
            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi= new ObjectInputStream(bi);
            newObj = oi.readObject();//目標對象
        } catch (IOException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return newObj;
    }
}

淺度複製類:
Java代碼
複製代碼 代碼如下:

import java.io.Serializable;
public class SimpleClone implements Cloneable ,Serializable {
    private static final long serialVersionUID = -7552721152118950502L;

    public    String s = null;
    public Object o = null;
    public CloneObject cloneObject = null;

    
    public SimpleClone(CloneObject cloneObject) {
        this.cloneObject = cloneObject;
    }
    public Object clone() {
        SimpleClone newSimpleClone =  null;
        try {
            /* 淺複製 */
            newSimpleClone = (SimpleClone)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return newSimpleClone;
    }
}

複製對象:
Java代碼
複製代碼 代碼如下:

import java.io.Serializable;

public class CloneObject implements Serializable{

    private static final long serialVersionUID = 4332788486669750696L;

    private String name ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

複製測試:
Java代碼
複製代碼 代碼如下:

public class TestClone {

    public static void main(String[] arg) {
        CloneObject obj1 = new CloneObject();
        obj1.setName("cloneOne");
        CloneObject obj2 = new CloneObject();
        obj2.setName("cloneTwo");

        SimpleClone simpleClone1 = new SimpleClone(obj1);
        SimpleClone simpleClone2 = new SimpleClone(obj2);

        simpleClone1.s = "simpleClone1";
        simpleClone1.o = "simpleClone1Object";

        //simpleClone2 = (SimpleClone)simpleClone1.clone();

        simpleClone2 = (SimpleClone)DepthClone.objectCopy(simpleClone1);

        /* 如果是淺複製, simpleClone1中的cloneObject會隨著simpleClone2的cloneObject改變面改變*/
        simpleClone2.cloneObject.setName("cloneThree");

        System.out.println(simpleClone1.cloneObject.getName());
        System.out.println(simpleClone2.cloneObject.getName());
    }

}

相關文章

聯繫我們

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