Java 常用类库 之 对象的克隆 Cloneable

http://www.verejava.com/?id=16993097143799

/**
    知识点: 对象的克隆 Cloneable
*/
public class TestClone {
    
    public static void main(String[] args) throws Exception {
        //实例化一只 喜洋洋
        Sheep sheep = new Sheep("喜洋洋", "白色");

        //灰太狼 想克隆两只 喜洋洋 就可以大吃一顿
        Sheep s1 = (Sheep) sheep.clone();
        Sheep s2 = (Sheep) sheep.clone();

        //输出克隆的两种羊
        System.out.println(s1.getName());
        System.out.println(s2.getName());
    }
}

class Sheep implements Cloneable {
    
    private String name;// 羊的名字
    private String color;//颜色

    public Sheep(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return this.name;
    }

    public String getColor() {
        return this.color;
    }

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}

http://www.verejava.com/?id=16993097143799

上一篇:EventBridge消息路由|高效构建消息路由能力


下一篇:Java 常用类库 之 Locale 国际化