java deepclone without implement cloneable interface

public Object deepClone() {    

   //将对象写到流里    

   ByteArrayOutoutStream bo=new ByteArrayOutputStream();    

   ObjectOutputStream oo=new ObjectOutputStream(bo);    

   oo.writeObject(this);    

   //从流里读出来    

   ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());    

   ObjectInputStream oi=new ObjectInputStream(bi);    

   return(oi.readObject());

这样做的前提是对象以及对象内部所有引用到的对象都是可串行化的.

你可能感兴趣的:(java deepclone without implement cloneable interface)