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

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

            

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

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


            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
           * 當沒有引用類型的變量時,為淺層次的拷貝
           *
           */
          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
           * 當沒有引用類型的變量時,為淺層次的克隆
           * 當有引用類型的變量時,為深層次的克隆
           */
          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 馮占科 閱讀(167) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 德州市| 西宁市| 葫芦岛市| 太仆寺旗| 哈尔滨市| 开原市| 宣汉县| 栾城县| 靖西县| 旅游| 哈尔滨市| 泾阳县| 临潭县| 彩票| 哈密市| 新民市| 临武县| 柳州市| 安丘市| 化州市| 桐城市| 鹿泉市| 金昌市| 安宁市| 临邑县| 通榆县| 板桥市| 丹棱县| 淮北市| 武乡县| 舞阳县| 庆元县| 伊吾县| 永春县| 潞西市| 昌黎县| 尚志市| 宜城市| 博罗县| 拜城县| 石河子市|