天天看點

設計模式--原型模式(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

繼續閱讀