<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    http://tomsui.blog.hexun.com/4308236_d.html
    方法(一) 引用拷貝

    Employee original = new Employee ("tomsui",4000);

    Employee copy = original ;

    original 與copy僅僅是同一對(duì)象的不同引用。

    ?

    方法(二) 淺克隆

    直接利用Object的clone()方法:

    protected Objectclone()?? throws CloneNotSupportedException

    Employee copy = (Employee)original.clone();

    注意兩點(diǎn):

    1) Object的clone() 是protected. 只能在包內(nèi)或子類調(diào)用.

    2) 如果淺克隆的對(duì)象中存在對(duì)象形式的成員變量:

    public class Employee

    {

    ??????? String name;

    ??????? int salary;

    ??????? Date birthday;

    }

    那么:

    ? Employee copy = (Employee) original.clone();

    只是拷貝了original對(duì)象中的基本類型和不變量.可變的對(duì)象成員變量拷貝得到的仍然是引用.

    不變量應(yīng)該包括(可以參見<java多線程設(shè)計(jì)模式>Immutable模式中界定的情況):

    a. String類對(duì)象

    b. 被final定義,子對(duì)象在生存周期中僅保存一些常量

    ?

    方法(三) 深克隆

    例子:

    class Employee implements Cloneable

    {

    ??????? private String name;

    ??????? private double salary;

    ??????? private Date birthday;

    ??????? // setter 與 getter

    ??????? public Object clone()

    ??????? {

    ??????????????? try

    ??????????????? {

    ??????????????????????? Employee cloned = (Employee ) super.clone();

    ??????????????????????? cloned.birthday = (Date) birthday.clone();

    ??????????????? }catch(CloneNotSupportedException e) {

    ??????????????? return null;

    ??????????????? }

    ??????? }

    }

    ?說明:

    1)Employee 必須實(shí)現(xiàn) Cloneable接口 (標(biāo)志接口)

    ?????標(biāo)志接口:完全的空接口。這里的作用是告訴JVM,類的設(shè)計(jì)者理解了cloneable()方法,可以通過isInstanceOf進(jìn)行運(yùn)行時(shí)檢查。

    2)覆寫的clone()方法必須定義為public (原是protected)

    3)clone()方法體必須在try-catch {}中,捕獲處理CloneNotSupportedException 。(防止類沒有實(shí)現(xiàn)Cloneable接口,正確實(shí)現(xiàn)了深克隆的話,這個(gè)異常肯定不會(huì)拋出)

    ps.?? JDK中,StringBuffer沒有覆寫clone()方法,雖然它確實(shí)繼承了Object的clone(),但在實(shí)際應(yīng)用中, 因?yàn)镾tringBuffer絕不會(huì)有子類,而且是在JDK外使用StringBuffer,所以被protected界定的clone()方法是完全不可見的!

    ?

    方法(四)使用序列化進(jìn)行克隆

    這種方法涉及IO操作,所以相對(duì)來講要比方法(三)慢.

    import java.io.*;
    import java.util.*;

    public class SerialCloneTest
    {?
    ?? public static void main(String[] args)
    ?? {?
    ????? Employee harry = new Employee("Harry Hacker", 35000,
    ???????? 1989, 10, 1);
    ????? // clone harry
    ????? Employee harry2 = (Employee)harry.clone();

    ????? // mutate harry
    ????? harry.raiseSalary(10);

    ????? // now harry and the clone are different
    ????? System.out.println(harry);
    ????? System.out.println(harry2);
    ?? }
    }

    /**
    ?? A class whose clone method uses serialization.
    */
    class SerialCloneable implements Cloneable, Serializable
    {?
    ?? public Object clone()
    ?? {?
    ????? try
    ????? {?
    ???????? // save the object to a byte array
    ???????? ByteArrayOutputStream bout = new
    ??????????? ByteArrayOutputStream();
    ???????? ObjectOutputStream out
    ??????????? = new ObjectOutputStream(bout);
    ???????? out.writeObject(this);
    ???????? out.close();

    ???????? // read a clone of the object from the byte array
    ???????? ByteArrayInputStream bin = new
    ??????????? ByteArrayInputStream(bout.toByteArray());
    ???????? ObjectInputStream in = new ObjectInputStream(bin);
    ???????? Object ret = in.readObject();
    ???????? in.close();

    ???????? return ret;
    ????? }?
    ????? catch (Exception e)
    ????? {?
    ???????? return null;
    ????? }
    ?? }
    }

    /**
    ?? The familiar Employee class, redefined to extend the
    ?? SerialCloneable class.
    */
    class Employee extends SerialCloneable
    {?
    ?? public Employee(String n, double s,
    ????? int year, int month, int day)
    ?? {?
    ????? name = n;
    ????? salary = s;
    ????? GregorianCalendar calendar
    ???????? = new GregorianCalendar(year, month - 1, day);
    ???????? // GregorianCalendar uses 0 for January
    ????? hireDay = calendar.getTime();
    ?? }

    ?? public String getName()
    ?? {?
    ????? return name;
    ?? }

    ?? public double getSalary()
    ?? {?
    ????? return salary;
    ?? }

    ?? public Date getHireDay()
    ?? {?
    ????? return hireDay;
    ?? }

    ?? public void raiseSalary(double byPercent)
    ?? {?
    ????? double raise = salary * byPercent / 100;
    ????? salary += raise;
    ?? }

    ?? public String toString()
    ?? {?
    ????? return getClass().getName()
    ???????? + "[name=" + name
    ???????? + ",salary=" + salary
    ???????? + ",hireDay=" + hireDay
    ???????? + "]";
    ?? }

    ?? private String name;
    ?? private double salary;
    ?? private Date hireDay;
    }

    ?

    ?方法(五) 其他方法

    可以通過java的反射機(jī)制定義一個(gè)類似于對(duì)象序列化的萬能克隆。改進(jìn)后再貼上來。

    posted on 2006-08-29 15:04 pear 閱讀(1033) 評(píng)論(0)  編輯  收藏 所屬分類: 技術(shù)
     
    主站蜘蛛池模板: 中文字幕成人免费视频| 久久国产精品免费| 毛片免费观看网址| 亚洲精品视频观看| 国产曰批免费视频播放免费s| 久久亚洲精品国产精品| 午夜免费福利片观看| 久久亚洲精品无码| 无码午夜成人1000部免费视频| 亚洲毛片在线观看| 114级毛片免费观看| 国产成人精品日本亚洲专| 最近中文字幕无吗免费高清| 亚洲GV天堂无码男同在线观看| 全部免费国产潢色一级| 黄页网站在线观看免费| 国产午夜亚洲精品午夜鲁丝片| 国产区在线免费观看| 久久精品国产亚洲av成人| 91福利免费体验区观看区| 亚洲三级在线免费观看| 国产免费av片在线无码免费看| 精品国产污污免费网站入口| 亚洲国产AV无码专区亚洲AV| 久久www免费人成看片| 亚洲人成自拍网站在线观看| 亚洲成A人片77777国产| 最近免费mv在线观看动漫| 亚洲色图.com| 国产yw855.c免费视频| 青青操视频在线免费观看| 亚洲综合亚洲国产尤物| 国产成人在线免费观看| 免费毛片在线看不用播放器| 亚洲国产精品人久久电影| 国产又大又粗又硬又长免费| 日本在线免费观看| 亚洲heyzo专区无码综合| 亚洲av永久无码精品网站| 成人免费视频网址| 国产三级在线免费|