java技術博客

          jsp博客
          數據加載中……
          CloneTest2.java

           

          /*
          *測試包含對象的克隆及clone方法的重寫
          */

          import java.util.GregorianCalendar;
          import java.util.Date;
          public class CloneTest{
          public static void main(String[] args){
          Student tom
          =new Student("tom","20020410");
          tom.setEntryDate(
          2002,01,02);
          Student tomcopy
          =(Student)tom.clone();
          tomcopy.setStudentSex(
          "man");
          tomcopy.setEntryDate(
          2003,10,23);
          System.out.println(tom.toString());
          System.out.println(tomcopy.toString());
          }

          }


          /*
           * 學生類,包括學生的基本信息,實現了Cloneable接口
           
          */

          class Student implements Cloneable
          {
           
          private String strName = "";//學生姓名
           private String strNumber = "";//學號
           private String strSex = "";//性別
           private String strBirthday = "";//出生年月
           private String strSpeciality = "";//專業
           private String strAddress = "";//地址
           private GregorianCalendar entryDate = new GregorianCalendar();//入學日期

           
          public Student(String name, String number)
           
          {
            strName 
          = name;
            strNumber 
          = number;
           }

          public Object clone(){
          try{
          Student cloned
          =(Student)super.clone();
          cloned.entryDate
          =(GregorianCalendar)entryDate.clone();
          return cloned;
          }

          catch(CloneNotSupportedException e){
          return null;
          }

          }



          public String getStudentName()
           
          {
            
          return strName;
           }


           
          public String getStudentNumber()
           
          {
            
          return strNumber;
           }


           
          public void setStudentSex(String sex)
           
          {
            strSex 
          = sex;
           }


           
          public String getStudentSex()
           
          {
            
          return strSex;
           }


           
          public String getStudentBirthday()
            
          {
            
          return strBirthday;
           }


           
          public void setStudentBirthday(String birthday)
           
          {
            strBirthday 
          = birthday;
           }


           
          public String getStudentSpeciality()
           
          {
            
          return strSpeciality;
           }


           
          public void setStudentSpeciality(String speciality)
           
          {
            strSpeciality 
          = speciality;
           }


           
          public String getStudentAddress()
           
          {
            
          return strAddress;
           }


           
          public void setStudentAddress(String address)
           
          {
            strAddress 
          = address;
           }

           
           
          public void setEntryDate(int year, int month, int day)
           
          {
            entryDate.set(year, month
          -1, day);
           }

           
           
          public Date getEntryDate()
           
          {
              
          return entryDate.getTime();
           }



          public String getStudentName()
           
          {
            
          return strName;
           }


           
          public String getStudentNumber()
           
          {
            
          return strNumber;
           }


           
          public void setStudentSex(String sex)
           
          {
            strSex 
          = sex;
           }


           
          public String getStudentSex()
           
          {
            
          return strSex;
           }


           
          public String getStudentBirthday()
            
          {
            
          return strBirthday;
           }


           
          public void setStudentBirthday(String birthday)
           
          {
            strBirthday 
          = birthday;
           }


           
          public String getStudentSpeciality()
           
          {
            
          return strSpeciality;
           }


           
          public void setStudentSpeciality(String speciality)
           
          {
            strSpeciality 
          = speciality;
           }


           
          public String getStudentAddress()
           
          {
            
          return strAddress;
           }


           
          public void setStudentAddress(String address)
           
          {
            strAddress 
          = address;
           }

           
           
          public void setEntryDate(int year, int month, int day)
           
          {
            entryDate.set(year, month
          -1, day);
           }

           
           
          public Date getEntryDate()
           
          {
              
          return entryDate.getTime();
           }

          public String toString(){
          String information
          ="student name="+strName+",student number="+strNumber;
          if(!strSex.euquals(""))
          information
          +=",birthday="+strBirthday;
          if(!strSpeciality.equals(""))
          information
          +=",專業="+strSpeciality;
          if(!strAddress.equals(""))
          information
          +=",address="+strAddress;
          if(entryDate!=null)
          information
          +=",entrydate="+getEntryDate();
          return information;
          }
          }


          posted on 2008-10-23 14:40 郭興華 閱讀(146) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 兴海县| 盐山县| 班戈县| 正阳县| 理塘县| 百色市| 玉树县| 久治县| 石林| 潜山县| 鹿邑县| 梓潼县| 大关县| 德令哈市| 武邑县| 宁安市| 松溪县| 宁都县| 金门县| 东台市| 镶黄旗| 尼勒克县| 大姚县| 上林县| 安达市| 梁河县| 青田县| 民和| 灵宝市| 泰州市| 蓝山县| 荥经县| 唐山市| 济阳县| 广南县| 麻栗坡县| 临猗县| 东海县| 鄢陵县| 田林县| 双牌县|