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


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


          網站導航:
           
          主站蜘蛛池模板: 沙田区| 邻水| 明溪县| 上犹县| 泰宁县| 渝北区| 庆云县| 盈江县| 准格尔旗| 瑞安市| 高密市| 普洱| 同江市| 长岛县| 沈丘县| 安龙县| 长宁县| 黄梅县| 郎溪县| 宁国市| 印江| 拉萨市| 新密市| 吉安市| 昌平区| 丹寨县| 峡江县| 晋州市| 安乡县| 大洼县| 通渭县| 常德市| 油尖旺区| 东源县| 镇宁| 沿河| 张家港市| 咸丰县| 梁平县| 澄迈县| 罗定市|