<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,  comments - 11,  trackbacks - 0

    How to clone:

    1. Implement the Cloneable interface, and

    2. Redefine the clone method with the public access modifier.


    Cloneable:

    The Cloneable interface is one of a handful of tagging interfaces that Java provides.A tagging interface has no methods; its only purpose is to allow the use of instanceof in a type inquiry:
    if (obj instanceof Cloneable) . . .
    We recommend that you do not use this technique in your own programs.


    Shallow copy:

    Even if the default (shallow copy) implementation of clone is adequate, you still need to implement the Cloneable interface, redefine clone to be public, and call super.clone(). Here is an example:

    class Employee implements Cloneable
    {
    ?? // raise visibility level to public, change return type
    ?? public Employee clone() throws CloneNotSupportedException
    ?? {
    ????? return super.clone();
    ?? }
    ?? . . .
    }


    Deep copy:

    class Employee implements Cloneable
    {
    ?? . . .
    ?? public Object clone() throws CloneNotSupportedException
    ?? {
    ????? // call Object.clone()
    ????? Employee cloned = (Employee) super.clone();

    ????? // clone mutable fields
    ????? cloned.hireDay = (Date) hireDay.clone()

    ????? return cloned;
    ?? }
    }

    1 The clone method of the Object class threatens to throw a CloneNotSupportedException—it does that whenever clone is invoked on an object whose class does not implement the Cloneable interface. Of course, the Employee and Date class implements the Cloneable interface, so the exception won't be thrown.

    2
    public Employee clone()
    {
    ?? try
    ?? {
    ????? return super.clone();
    ?? }
    ?? catch (CloneNotSupportedException e) { return null; }
    ?? // this won't happen, since we are Cloneable
    }

    This is appropriate for final classes. Otherwise, it is a good idea to leave the tHRows specifier in place. That gives subclasses the option of throwing a CloneNotSupportedException if they can't support cloning.


    Use clone:

    public static void main(String[] args) {
    ???? try {
    ??????? Employee original = new Employee("John Q. Public", 50000);
    ??????? original.setHireDay(2000, 1, 1);
    ??????? Employee copy = original.clone();
    ??????? copy.raiseSalary(10);
    ??????? copy.setHireDay(2002, 12, 31);
    ??????? System.out.println("original=" + original);
    ??????? System.out.println("copy=" + copy);
    ??? }
    ??? catch (CloneNotSupportedException e) {
    ??????? e.printStackTrace();
    ??? }
    }

    posted on 2007-11-10 10:39 jadmin 閱讀(75) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产免费阿v精品视频网址| 黄色免费网址大全| 无码人妻一区二区三区免费n鬼沢| 亚洲精品高清在线| 猫咪免费人成网站在线观看入口 | 91亚洲va在线天线va天堂va国产| a级毛片视频免费观看| 国产亚洲成av片在线观看| a级毛片免费播放| 午夜亚洲www湿好大| 伊人久久免费视频| 亚洲国产av美女网站| 成人性生免费视频| 国产亚洲综合久久| 国产专区一va亚洲v天堂| 国产又黄又爽又大的免费视频| 久久精品国产亚洲综合色 | 动漫黄网站免费永久在线观看| 99亚偷拍自图区亚洲| 免费一级特黄特色大片在线观看| 免费人成再在线观看网站| 亚洲日韩小电影在线观看| 日韩精品无码一区二区三区免费| 亚洲国产精品人久久电影| 成年性羞羞视频免费观看无限| 亚洲av综合日韩| 亚洲深深色噜噜狠狠爱网站| 亚洲一区二区在线免费观看| 亚洲日本乱码卡2卡3卡新区| 亚洲成av人片不卡无码久久| 久久免费观看国产精品88av| 色偷偷亚洲女人天堂观看欧| 亚洲精品无码永久在线观看| 午夜免费福利小电影| 亚洲欧美成人一区二区三区| 中文字幕不卡亚洲| 国产啪精品视频网免费| 曰批全过程免费视频免费看 | 亚洲综合久久1区2区3区| 免费鲁丝片一级观看| 97人妻精品全国免费视频|