<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)航:
     
    主站蜘蛛池模板: 一日本道a高清免费播放| 特黄特色大片免费| 99久久免费精品高清特色大片| 久久精品国产亚洲Aⅴ香蕉| 丰满妇女做a级毛片免费观看| 亚洲精品97久久中文字幕无码| 特级毛片aaaa免费观看| 亚洲福利中文字幕在线网址| 亚洲精品视频免费| 亚洲人成色77777| 一级毛片免费观看不卡视频| 亚洲人色大成年网站在线观看| 一个人免费观看在线视频www| 亚洲国产日韩a在线播放| 免费人成在线观看播放国产| 一区视频免费观看| 亚洲精品高清视频| 黄页网站免费观看| 亚洲a∨无码一区二区| 亚洲精品尤物yw在线影院| 97无码人妻福利免费公开在线视频| 亚洲人成网www| 成人午夜视频免费| 九一在线完整视频免费观看| 亚洲成色www久久网站夜月| 日韩视频在线精品视频免费观看| 亚洲伊人久久大香线蕉结合| 国产一级大片免费看| 中国一级特黄高清免费的大片中国一级黄色片 | 久久久久高潮毛片免费全部播放 | 最近中文字幕大全免费版在线| 久久精品九九亚洲精品| 毛片a级毛片免费播放下载| 免费人成大片在线观看播放| 亚洲AV永久青草无码精品| 在线a级毛片免费视频| 久久久久久久久久久免费精品| 91亚洲精品第一综合不卡播放| 免费看美女被靠到爽的视频| 久久久久久国产a免费观看不卡| 亚洲伊人久久大香线蕉影院|