深克隆核心代码:
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;
}
原文地址:https://www.cnblogs.com/jyfby/p/8949836.html
时间: 2024-10-09 01:43:17