隨筆-159  評論-114  文章-7  trackbacks-0

          某個類要想實現拷貝的功能,就必須實現Cloneable接口,并覆蓋Object的clone()方法,才能真正實現克隆。

          淺拷貝,一種默認的實現,Teacher類中的clone方法的實現就是淺拷貝。

          Student類的clone方法就是深拷貝。注意super.clone返回的對象實際上是被類的對象,可以放心強制轉換,至于為什么,我也不知道,估計得讀虛擬機規范,從Object源碼看也不到什么,因為是protect native Object clone();


          import java.util.*;
          public class TestClone{
           
          public static void main(String[] args)
           
          {
            Teacher t 
          = new Teacher("Name",22);
            Teacher m 
          = (Teacher)t.clone();
            System.out.println(m);
            
            Student stu 
          = new Student();
            Student deepcloneSTU 
          = (Student)stu.clone();
            stu.courses.put(
          new Integer(1),"Math");
            deepcloneSTU.courses.put(
          new Integer(100),"Java");
            disp(stu.courses);
            disp(deepcloneSTU.courses);
            
           }

           
           
          static void disp(HashMap h)
           
          {
            Set keySet 
          = h.keySet();
            Iterator it 
          = keySet.iterator();
            
          while(it.hasNext())
            
          {
             System.out.println(h.get(it.next()));
            }

           }

          }


          class Teacher implements Cloneable{
           String name;
           
          int age;
           Teacher(String name,
          int age)
           
          {
            
          this.name = name;
            
          this.age = age;
           }

           
           
          public Object clone()
           
          {
            
          try{
             
          return super.clone();
            }
          catch(CloneNotSupportedException e)
            
          {
             
          throw new Error("This should never happen!"); 
            }

           }

           
           
          public String toString()
           
          {
            
          return name + "  " + age;
           }

          }


          class Student implements Cloneable{
           HashMap courses 
          = new HashMap();
           Student()
          {} 
           
           
          public Object clone()
           
          {
            
          try{
             Student stu 
          = (Student)super.clone();
             stu.courses 
          = (HashMap)courses.clone();
             
          return stu;
            }
          catch(CloneNotSupportedException e)
            
          {
             
          throw new Error("This should never happen!"); 
            }

           }

          }




          如何通過對象串行化的方式來做進行拷貝工作呢?

          import java.util.*;
          import java.io.*;
          public class TestClone{
              
          public static void main(String[] args) throws Exception
              
          {
                  BMW mycar 
          = new BMW();
                  ByteArrayOutputStream memoryOutputStream 
          = new ByteArrayOutputStream();
                  ObjectOutputStream serializer 
          = new ObjectOutputStream(memoryOutputStream);
                  serializer.writeObject(mycar);
                  serializer.flush();
                  
                  ByteArrayInputStream memoryInputStream 
          = new ByteArrayInputStream(memoryOutputStream.toByteArray( ));
                  ObjectInputStream deserializer 
          = new ObjectInputStream(memoryInputStream);
                  BMW mycopycar 
          = (BMW)deserializer.readObject();

                  mycar.add(
          new String("NB"));
                  mycopycar.add(
          new String("NBNB"));
                System.out.println(mycar.hashCode());
                System.out.println(mycopycar.hashCode());
                System.out.println(mycar.equals(mycopycar));
              }

          }


          class BMW implements Serializable
          {
              
          private int wheels;
            
          private String model;
            
          private ArrayList forTest;
            
            BMW()
            
          {
                wheels 
          = 4;
                model 
          = "530i";
                forTest 
          = new ArrayList();
            }

            
            
          public void add(Object o)
            
          {
                forTest.add(o);
            }

            
            
          public String toString()
            
          {
                
          return "WHEEL:" + wheels + "MODEL:" + model + forTest.toString();    
            }

            
            
          public int hashCode()
            
          {
                
          return wheels + model.hashCode() + forTest.hashCode();    
            }

            
            
          public boolean equals(Object o)
            
          {
                
          if(o == this)
                    
          return true;
                
          if(o == null)
                    
          return false;
                
          if(!(o instanceof BMW))
                  
          return false;
                BMW bmw 
          = (BMW)o;
                
          if(bmw.wheels == wheels&&bmw.model.equals(model)&&bmw.forTest.equals(forTest))
                    
          return true;
                
          else return false;
            }
            
          }


          記住,如果覆蓋了equals方法,應該也覆蓋hashCode(),因為如果兩個對象相等也就是equals()返回true,那么這兩個對象應該有相同的hashCode。



          posted on 2006-01-08 17:15 北國狼人的BloG 閱讀(3606) 評論(0)  編輯  收藏 所屬分類: 達內學習總結
          主站蜘蛛池模板: 四子王旗| 秦皇岛市| 苏尼特右旗| 都兰县| 宁明县| 泰兴市| 景德镇市| 泸溪县| 射洪县| 合阳县| 贵南县| 儋州市| 诸暨市| 阿克| 郁南县| 金华市| 贞丰县| 巢湖市| 万山特区| 澄江县| 邢台市| 长沙市| 剑河县| 交城县| 宝丰县| 望谟县| 鹿泉市| 阜新市| 桓台县| 偃师市| 开远市| 旬阳县| 炉霍县| 隆子县| 扎囊县| 平舆县| 扬中市| 凉山| 靖远县| 吉木萨尔县| 斗六市|