<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) 評(píng)論(0)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲欧洲无码AV不卡在线| 久久国产乱子伦精品免费午夜 | 蜜桃成人无码区免费视频网站 | 国产亚洲午夜精品| 亚洲精品天堂成人片?V在线播放| 亚洲欧洲∨国产一区二区三区| 99精品视频在线观看免费专区 | av无码东京热亚洲男人的天堂 | 中文字幕在线亚洲精品| 亚洲人成色777777老人头| 日本成人免费在线| a在线免费观看视频| 吃奶摸下高潮60分钟免费视频| aa级女人大片喷水视频免费| 精品亚洲成a人片在线观看少妇 | 免费国产a国产片高清网站| 四虎国产精品永久免费网址| 亚洲av无码专区国产不乱码| 亚洲国产综合91精品麻豆| 国产精品无码素人福利免费| 免费无遮挡无码永久视频| 无遮挡a级毛片免费看| 亚洲日本香蕉视频观看视频| 亚洲人成网站18禁止一区| 99精品国产免费久久久久久下载 | 另类专区另类专区亚洲| 久久久国产精品亚洲一区| 亚洲精品国自产拍在线观看| 18国产精品白浆在线观看免费| 国产高潮久久免费观看| 久久精品国产亚洲麻豆| 国产在线观看www鲁啊鲁免费| 最近中文字幕大全中文字幕免费| 亚洲丰满熟女一区二区v| 亚洲乱码国产乱码精品精| 国产免费人视频在线观看免费 | 久久精品国产亚洲夜色AV网站| 国产特级淫片免费看| 皇色在线视频免费网站| 最近免费视频中文字幕大全| 中文字幕无码免费久久9一区9|