欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解析JAVA深度克隆与浅度克隆的区别详解

程序员文章站 2023-12-12 16:53:22
在java克隆对象不能简单的使用clone方法,clone方法只是进行浅克隆。请看下方:深度克隆类:java代码 复制代码 代码如下:import java.io.byte...
在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());
    }

}

上一篇:

下一篇: