<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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 閱讀(3614) 評論(0)  編輯  收藏 所屬分類: 達內學習總結
    主站蜘蛛池模板: 久久亚洲国产精品五月天| 亚洲精品一卡2卡3卡四卡乱码 | 99xxoo视频在线永久免费观看| 久久久亚洲欧洲日产国码是AV| 在线jyzzjyzz免费视频| 有码人妻在线免费看片| 亚洲精品一卡2卡3卡三卡四卡| 成年丰满熟妇午夜免费视频| eeuss影院www天堂免费| 亚洲国产成人精品无码区在线秒播| 国产男女性潮高清免费网站| 免费精品99久久国产综合精品| 亚洲综合无码一区二区痴汉| 在线观看亚洲成人| 操美女视频免费网站| 精品亚洲永久免费精品| 亚洲人av高清无码| 亚洲AV无码成人精品区天堂| 国产又大又黑又粗免费视频 | 日韩一区二区免费视频| 成人A片产无码免费视频在线观看| 亚洲人成网站看在线播放| 亚洲欧洲日产国码av系列天堂 | 精品一卡2卡三卡4卡免费视频| 亚洲影院天堂中文av色| 亚洲精品色午夜无码专区日韩| 在线观看免费毛片| 99免费视频观看| 国产精品免费看久久久香蕉| 中文字幕无码精品亚洲资源网久久 | 国产V亚洲V天堂无码| 国产精品无码素人福利免费| 99re6热视频精品免费观看| a高清免费毛片久久| 亚洲暴爽av人人爽日日碰| 亚洲视频在线观看| 亚洲毛片αv无线播放一区| 国产乱子伦片免费观看中字| 免费精品国产自产拍在线观看图片| 永久免费AV无码网站国产| 特级毛片爽www免费版|