<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)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 久九九精品免费视频| 青草久久精品亚洲综合专区| 2021在线永久免费视频| 亚洲中文字幕久在线| 国产成人无码免费视频97| 又粗又长又爽又长黄免费视频 | 毛片a级毛片免费播放下载| 国产精品亚洲综合| 亚洲成人午夜在线| 欧美最猛性xxxxx免费| 国产午夜成人免费看片无遮挡| 亚洲一级毛片免费在线观看| 亚洲成a人片在线观看老师| 国产91精品一区二区麻豆亚洲| 3d成人免费动漫在线观看| 久久久久久国产精品免费无码| 国产精品亚洲色图| 免费看又黄又爽又猛的视频软件| 久久久婷婷五月亚洲97号色| 国产色婷婷精品免费视频| 又粗又黄又猛又爽大片免费| 青青青国产在线观看免费| 成人毛片免费观看视频在线| 国产国产人免费视频成69大陆| 亚洲国产精品丝袜在线观看| 亚洲色婷婷综合开心网| 亚洲国产精品va在线播放| 青青草国产免费久久久下载| 最刺激黄a大片免费网站| 一二三四视频在线观看中文版免费| 日韩精品无码免费专区午夜| 69免费视频大片| 日韩免费一级毛片| 无人在线观看完整免费版视频| 国产福利免费在线观看| 国产中文在线亚洲精品官网| 99精品一区二区免费视频| 日本在线看片免费| 黄色视频在线免费观看| 黄页网站在线免费观看| 国产精品手机在线亚洲|