package com.leitan.architect.pattern.prototype.deep;
import java.io.*;
import java.util.Date;
public class QiTianDaSheng extends Monkey implements Cloneable, Serializable {
public JinGuBang jinGuBang;// 金箍棒
public QiTianDaSheng() {
this.birthday = new Date();
this.jinGuBang = new JinGuBang();
}
@Override
protected Object clone() throws CloneNotSupportedException {
// return super.clone();// 浅拷贝
return this.deepClone();// 深拷贝
}
private Object deepClone() {
QiTianDaSheng daSheng = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
daSheng = (QiTianDaSheng) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return daSheng;
}
}
最简单的深拷贝_飘云羽逸_新浪博客
最新推荐文章于 2024-04-01 00:11:03 发布