<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 閱讀(78) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲人妻av伦理| 国产一级做a爱免费视频| 亚洲乱码精品久久久久..| 人体大胆做受免费视频| 国产精品国产自线拍免费软件| 亚洲午夜一区二区电影院| 57pao国产成永久免费视频| 久久亚洲私人国产精品vA| 51精品视频免费国产专区| 自怕偷自怕亚洲精品| 免费观看激色视频网站(性色)| 精品亚洲成AV人在线观看| 亚洲成人免费网站| 亚洲AV一二三区成人影片| 免费人成视频在线| 亚洲欧美乱色情图片| 四虎永久在线免费观看| 一级特级女人18毛片免费视频| 国产亚洲精aa成人网站| 免费网站看av片| 亚洲视频精品在线观看| 野花高清在线电影观看免费视频 | 黄色一级视频免费| 日本xxwwxxww在线视频免费 | 亚洲一级黄色大片| 在线免费观看视频你懂的| 综合偷自拍亚洲乱中文字幕| 丝袜熟女国偷自产中文字幕亚洲| 国产免费阿v精品视频网址| 久久久亚洲欧洲日产国码是AV| 嫖丰满老熟妇AAAA片免费看| 亚洲成av人片在www鸭子| 亚洲美女在线国产| 日本免费中文视频| 亚洲中文无码永久免| 亚洲第一精品电影网| 四虎成人免费网站在线| a免费毛片在线播放| 亚洲福利电影在线观看| 国产gav成人免费播放视频| 免费成人在线电影|