原型模式的深克隆和浅克隆

深克隆核心代码:

public Object deepclone () throws IOException, ClassNotFoundException{
//将对象写入流中
ByteArrayOutputStream bao=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bao);
oos.writeObject(this);

//将对象从流中取出
ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bis);
return(ois.readObject());
}

浅克隆核心代码:

ublic Object clone(){
Email clone=null;

try {
clone=(Email)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
System.out.println("Clone failure");
}
return clone;
}

猜你喜欢

转载自www.cnblogs.com/jyfby/p/8949836.html