JAVA的深浅拷备

package com.jd.ng.shiro.testFactory;

import java.io.*;

/**
* @author wangzhilei
* @Author: husToy.Wang
* @Date: 2019/10/30 15:20
* @Version 1.0
* @date 2019-10-30 15:21
*/
public class ProtoType implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;

private String string;

private SerializableObject obj;

/*浅copy*/
@Override
protected Object clone() throws CloneNotSupportedException {
return (ProtoType) super.clone();
}

/* 深复制 */
public Object deepClone() throws IOException, ClassNotFoundException {

/* 写入当前对象的二进制流 */
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 缓冲区
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);


/* 读出二进制流产生的新对象 */
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();

}


public static long getSerialVersionUID() {
return serialVersionUID;
}

public String getString() {
return string;
}

public SerializableObject getObj() {
return obj;
}
}


class SerializableObject implements Serializable {
private static final long serialVersionUID = 1L;
}
posted @ 2019-10-30 16:46  雷神约  阅读(176)  评论(0编辑  收藏  举报