A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Anmyre 中级黑马   /  2015-9-22 09:51  /  638 人查看  /  2 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

原型模式(ProtoType) 通过一个原型对象来创建一个新对象(克隆)。Java中要给出Clonable接口的实现,具体类要实现这个接口,并给出clone()方法的实现细节,这就是简单原型模式的应用。  浅拷贝:只拷贝简单属性的值和对象属性的地址  深拷贝:拷贝本对象引用的对象,有可能会出现循环引用的情况。可以用串行化解决深拷贝。写到流里再读出来,这时会是一个对象的深拷贝结果。


[java] view plaincopy


  • import java.io.*;  
  •   
  • public class TestClonealbe {  
  •   
  • public static void main(String[] args) throws Exception {  
  •   
  • Father f=new Father();  
  •   
  •   
  •   
  • User u1=new User("123456",f);  
  •   
  • User u2=(User)u1.clone();  
  •   
  • System.out.println(u1==u2);  
  •   
  • System.out.println(u1.f==u2.f);  
  •   
  • }  
  •   
  • }  
  •   
  • class User implements Cloneable,Serializable{  
  •   
  • String password;  
  •   
  • Father f;  
  •   
  • public User(String password,Father f){  
  •   
  • this.password=password;  
  •   
  • this.f=f;  
  •   
  • }  
  •   
  • public Object clone() throws CloneNotSupportedException {  
  •   
  • //return super.clone();  
  •   
  • ObjectOutputStream out=null;  
  •   
  • ObjectInputStream in=null;  
  •   
  • try {  
  •   
  • ByteArrayOutputStream bo=new ByteArrayOutputStream();  
  •   
  • out = new ObjectOutputStream(bo);  
  •   
  • out.writeObject(this);  
  •   
  • out.flush();  
  •   
  • byte[] bs=bo.toByteArray();  
  •   
  •   
  •   
  • ByteArrayInputStream bi=new ByteArrayInputStream(bs);  
  •   
  • in = new ObjectInputStream(bi);  
  •   
  • Object o=in.readObject();  
  •   
  •   
  •   
  • return o;  
  •   
  • } catch (IOException e) {  
  •   
  • e.printStackTrace();  
  •   
  • return null;  
  •   
  • } catch (ClassNotFoundException e) {  
  •   
  • e.printStackTrace();  
  •   
  • return null;  
  •   
  • }  
  •   
  • finally{  
  •   
  • try {  
  •   
  • out.close();  
  •   
  • in.close();  
  •   
  • } catch (IOException e) {  
  •   
  • e.printStackTrace();  
  •   
  • }  
  •   
  • }  
  •   
  • }  
  •   
  • }  
  •   
  • class Father implements Serializable{}  


2 个回复

倒序浏览
看着不大理解,先收藏
回复 使用道具 举报
王鲁悦 来自手机 中级黑马 2015-9-22 11:18:06
藤椅
先收藏着
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马