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 郭興華 閱讀(147) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 本溪| 岫岩| 抚远县| 惠东县| 平山县| 太仆寺旗| 东乡县| 云梦县| 诸暨市| 万年县| 远安县| 余干县| 定南县| 怀来县| 出国| 大关县| 永春县| 木里| 天峨县| 涟源市| 台北市| 应城市| 通州区| 龙南县| 师宗县| 菏泽市| 惠水县| 金山区| 万盛区| 马边| 晋城| 昌乐县| 黔西县| 同德县| 孝感市| 东明县| 稻城县| 吴川市| 大悟县| 广丰县| 怀远县|