灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2587回复:0

java对象克隆clone

楼主#
更多 发布于:2012-09-08 09:46

/**
  * 克隆
  *
  * @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的专栏


喜欢0 评分0
游客

返回顶部