Cloning of objects of common Java libraries cloneable

Source: Internet
Author: User

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

Cloning of objects of common Java libraries cloneable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.