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

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


    網站導航:
     
    主站蜘蛛池模板: 99在线精品免费视频九九视| 精品熟女少妇aⅴ免费久久| 1000部夫妻午夜免费| 亚洲伊人久久大香线蕉苏妲己| 免费无码H肉动漫在线观看麻豆| 亚洲国产一区视频| 一级毛片成人免费看a| 亚洲精品高清在线| 中文字幕手机在线免费看电影 | 国产精品久久香蕉免费播放| 亚洲一级特黄特黄的大片| 无人影院手机版在线观看免费| 亚洲高清一区二区三区| 在线a人片天堂免费观看高清| 色欲aⅴ亚洲情无码AV蜜桃| 免费女人18毛片a级毛片视频| selaoban在线视频免费精品| 亚洲va无码专区国产乱码| 天天影院成人免费观看| 亚洲精品无码专区| 亚洲高清成人一区二区三区| 国产在线观看无码免费视频| 亚洲高清视频在线播放| 成年性午夜免费视频网站不卡| 国产天堂亚洲国产碰碰| 亚洲国产精品VA在线观看麻豆 | 亚洲欧洲中文日韩av乱码| 少妇性饥渴无码A区免费 | 亚洲黄网在线观看| 在线观看国产情趣免费视频| 国产精品免费久久久久久久久| 亚洲国产精品无码久久一线| 在线观看无码AV网站永久免费| 婷婷亚洲综合五月天小说在线| 亚洲狠狠婷婷综合久久久久| 国产精品永久免费10000| 国产精品亚洲一区二区三区在线观看| 国产亚洲老熟女视频| 成年免费大片黄在线观看岛国 | 日本在线免费播放| 亚洲欧美国产欧美色欲|