当前位置:编程学习 > JAVA >>

java对象克隆clone

/**
  * 克隆
  * 
  * @param <T>
  * @param t
  * @return
  */ 
 public static final <T> T clone(T t) { 
  try { 
   ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
   ObjectOutputStream out = new ObjectOutputStream(baos); 
   out.writeObject(t); 
   out.close(); 
   ByteArrayInputStream bin = new ByteArrayInputStream(baos 
     .toByteArray()); 
   ObjectInputStream in = new ObjectInputStream(bin); 
   Object clone = in.readObject(); 
   in.close(); 
   return (T) (clone); 
  } catch (ClassNotFoundException e) { 
   throw new InternalError(e.toString()); 
  } catch (StreamCorruptedException e) { 
   throw new InternalError(e.toString()); 
  } catch (IOException e) { 
   throw new InternalError(e.toString()); 
  } 
 } 

 

 

摘自 lpdx111的专栏

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,