程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 解析JAVA深度克隆與淺度克隆的差別詳解

解析JAVA深度克隆與淺度克隆的差別詳解

編輯:關於JAVA

解析JAVA深度克隆與淺度克隆的差別詳解。本站提示廣大學習愛好者:(解析JAVA深度克隆與淺度克隆的差別詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是解析JAVA深度克隆與淺度克隆的差別詳解正文


在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());
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved