隨筆-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 閱讀(3607) 評論(0)  編輯  收藏 所屬分類: 達內學習總結
          主站蜘蛛池模板: 定日县| 孝义市| 大埔区| 牟定县| 通许县| 丹东市| 九龙坡区| 潜山县| 嵊州市| 平邑县| 永吉县| 陈巴尔虎旗| 碌曲县| 砚山县| 微山县| 建湖县| 论坛| 崇义县| 阜新市| 昌宁县| 乌兰浩特市| 那曲县| 无锡市| 黎平县| 关岭| 凤冈县| 竹北市| 乌鲁木齐市| 漳平市| 班戈县| 永平县| 榆中县| 宁陕县| 共和县| 静乐县| 阜平县| 临安市| 安新县| 永登县| 平邑县| 河东区|