Prototype设计模式

            浅克隆无法对对象中包含的其他非java原是类型数据进行复制。如果需要复制其他非java原是类型数据,则需要使用深克隆方式,对象以及其内部所有对象必须实现java.io.Serializable接口,clone方法改写成以下形式:

  1. public Object clone(){     
  2.      
  3.         try {     
  4.      
  5.             ByteArrayOutputStream ot = new ByteArrayOutputStream();     
  6.      
  7.             ObjectOutputStream oo = new ObjectOutputStream(ot);     
  8.      
  9.             oo.writeObject(this);     
  10.      
  11.             ByteArrayInputStream it = new ByteArrayInputStream(ot.toByteArray());     
  12.      
  13.             ObjectInputStream oi = new ObjectInputStream(it);     
  14.      
  15.             return (oi.readObject());     
  16.      
  17.         } catch (IOException e) {     
  18.             e.printStackTrace();     
  19.             return null;     
  20.         } catch (ClassNotFoundException e) {     
  21.             e.printStackTrace();     
  22.             return null;     
  23.         }     
  24.      
  25.     }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值