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

          常用鏈接

          留言簿

          隨筆分類(lèi)

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

            

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

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


            Student s1 = new Student("gaoer", 14);
            System.out.println("s1name=" + s1.getName());
            
            Student s2 = s1; // 未使用clone,他倆使用一個(gè)地址,
            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)沒(méi)有引用類(lèi)型的變量時(shí),為淺層次的拷貝
           *
           */
          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)沒(méi)有引用類(lèi)型的變量時(shí),為淺層次的克隆
           * 當(dāng)有引用類(lèi)型的變量時(shí),為深層次的克隆
           */
          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) 評(píng)論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南涧| 长治县| 沁阳市| 十堰市| 红河县| 兴文县| 仁怀市| 盐城市| 综艺| 永丰县| 大化| 武平县| 安溪县| 色达县| 卢龙县| 仙游县| 蓝田县| 鄂伦春自治旗| 汉中市| 安图县| 漳浦县| 桓台县| 连州市| 清远市| 阳东县| 崇义县| 桑植县| 池州市| 红桥区| 察哈| 永川市| 洞口县| 武邑县| 如皋市| 礼泉县| 上杭县| 拉孜县| 石家庄市| 古浪县| 扶沟县| 中宁县|