GOF创建型模式:原型模式

浅克隆:

import com.sun.jndi.cosnaming.IiopUrl;

class Address{

        }

public class Customer implements Cloneable {
    private Address address =null;
    public Customer(){
        this.address=new Address();
    }

    public Object clone(){
        Object obj =null;
        try{
            obj=super.clone();
        }catch (Exception e){
            System.out.println("clone failure");
        }
        return obj;
    }
}

深克隆:
class customer implements Serializable{
    private Address address =null;
    public customer(){
        this.address= new Address();
    }
    public Object deepClone() throws Exception{
        ByteArrayOutputStream bao =new ByteArrayOutputStream();
        ObjectOutputStream os =new ObjectOutputStream(bao);
        os.writeObject(this);
        ByteArrayInputStream bis =new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois =new ObjectInputStream(bis);
        return (ois.readObject());
    }

}

实验结果分析
浅克隆中,Object类充当抽象原型类,Customer类充当具体原型类,浅克隆只复制容器对象,不复制成员对象。
深克隆,Customer和Address实现了Serializable接口,深克隆既要复制容器对象,又要复制成员对象。

全部评论

相关推荐

asdasdasda...:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
双非有机会进大厂吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务