天天看点

设计模式--原型模式(2)--深拷贝&浅拷贝

设计模式–原型模式(1)

https://blog.csdn.net/yangyanjava/article/details/106790859

浅克隆

1:直接调用clone方式

public class Sheep implements Cloneable {
    String name;
    Sheep friends;

    Sheep(String name) {
        this.name = name;
    }


    @Override
    protected Sheep clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println(e.getMessage());
        }

        return sheep;
    }

}

           
设计模式--原型模式(2)--深拷贝&浅拷贝

内部的应用数据类型的对象并没有重新创建,而是指向第一个对象的成员变量

深克隆

1:另外一个类重写clone方法,在继承这个类

2:使用对象序列化读写流的方式克隆

深入了解可查看:

https://blog.csdn.net/riemann_/article/details/87217229

继续阅读