码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-06-23 11:45:59      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:href   col   object c   imp   克隆   return   TE   ble   out   

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

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

标签:href   col   object c   imp   克隆   return   TE   ble   out   

原文地址:https://www.cnblogs.com/verejava/p/9216573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!