隨筆 - 24  文章 - 0  trackbacks - 0
          <2011年1月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

            

          為了獲取對象的一份拷貝,我們可以利用Object類的clone()方法。
          在派生類中覆蓋基類的clone()方法,并聲明為public。
          在派生類的clone()方法中,調(diào)用super.clone()。
          在派生類中實現(xiàn)Cloneable接口。

          為什么我們在派生類中覆蓋Object的clone()方法時,一定要調(diào)用super.clone()呢?在運行時刻,Object中的clone()識別出你要復(fù)制的是哪一個對象,然后為此對象分配空間,并進行對象的復(fù)制,將原始對象的內(nèi)容一一復(fù)制到新對象的存儲空間中。


            Student s1 = new Student("gaoer", 14);
            System.out.println("s1name=" + s1.getName());
            
            Student s2 = s1; // 未使用clone,他倆使用一個地址,
            s2.setAge(12);
            s2.setName("zhangsan");
            System.out.println("s1name=" + s1.getName());

            Student s3 = (Student) s1.clone();
            s3.setAge(12);
            s3.setName("lisi");
            System.out.println("s1name=" + s1.getName());


          package l4;
          /**
           *
           * @author Administrator
           * 當(dāng)沒有引用類型的變量時,為淺層次的拷貝
           *
           */
          public class Student implements Cloneable {
           private String name;
           private int age;

           public Student(String name, int age) {

            this.name = name;
            this.age = age;
           }

           public String getName() {
            return name;
           }

           public void setName(String name) {
            this.name = name;
           }

           public int getAge() {
            return age;
           }

           public void setAge(int age) {
            this.age = age;
           }

           protected Object clone() throws CloneNotSupportedException {

            Object o = null;
            o = super.clone();
            return o;
           }

          }







          package l4;
          /**
           *
           * @author Administrator
           * 當(dāng)沒有引用類型的變量時,為淺層次的克隆
           * 當(dāng)有引用類型的變量時,為深層次的克隆
           */
          public class Student implements Cloneable {
           private String name;
           private int age;
           Point pt;

           public Student(String name, int age) {

            this.name = name;
            this.age = age;
           }
           public Student(String name, int age,Point pt) {

            this.name = name;
            this.age = age;
            this.pt = pt;
           }


           public String getName() {
            return name;
           }

           public void setName(String name) {
            this.name = name;
           }

           public int getAge() {
            return age;
           }

           public void setAge(int age) {
            this.age = age;
           }

           protected Object clone() throws CloneNotSupportedException {

            Student o = null;
            o = (Student)super.clone();
            o.pt = (Point)pt.clone();
            return o;
           }

          }




          package l4;

          public class Point implements Cloneable {
           public int x;
           public int y;

           @Override
           public String toString() {
            return "x=" + x + "y=" + y;
           }

           protected Object clone() throws CloneNotSupportedException {
            Object o = null;
            o = super.clone();
            return o;
           }
          }


          posted on 2011-01-04 22:16 馮占科 閱讀(168) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南京市| 抚远县| 定兴县| 奉新县| 普兰店市| 铜川市| 永登县| 万荣县| 奇台县| 会同县| 施秉县| 外汇| 运城市| 临颍县| 周口市| 霞浦县| 枣庄市| 浦东新区| 鞍山市| 怀柔区| 鄂伦春自治旗| 麻阳| 元阳县| 马龙县| 聊城市| 玉环县| 区。| 远安县| 宁夏| 双柏县| 平和县| 巧家县| 莲花县| 彰化县| 惠安县| 彭山县| 大姚县| 灵寿县| 牙克石市| 吉林省| 云和县|