java技術博客

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

           

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

          public class CloneTest{
          public static void main(String[] args){
          Student tom
          =new Student("tom","20020410");
          Student tomcopy
          =(Student)tom.clone();
          tomcopy.setStudentSex(
          "man");
          tomcopy.setStudentAddress(
          "America");
          System.out.println(tom);
          System.out.println(tomcopy);
          }

          }

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

          class Student implements Cloneable
          {
           
          private String strName = "";//學生姓名
           private String strNumber = "";//學號
           private String strSex = "";//性別
           private String strBirthday = "";//出生年月
           private String strSpeciality = "";//專業
           private String strAddress = "";//地址
           
           
          public Student(String name, String number)
           
          {
            strName 
          = name;
            strNumber 
          = number;
           }

          public Object clone(){
          try{
          return super.clone();
          }

          catch(CloneNotSupportedException e){
          return null;}

          }

          public String getStudentName(){
          return strNumber;
          }

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

          public String getStudentSex(){
          return strSex;
          }

          public String getStudentBirthday(){
          return strBirthday;
          }

          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 Stirng toString()
          {
          String information
          ="student name="+strName+",student number="+strNumber;
          if(!strSex.equals(""))
          information
          +=",sex="+strSex;
          if(!strBirthday.equals(""))
          information
          +=",birthday="+strBirthday;
          if(!strSpeciality.equals(""))
          information
          +=",專業="+strSpeciality;
          if(!strAddress.equals(""))
          information
          +=",address="+strAddress;
          return information;
          }

          }

          }

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


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


          網站導航:
           
          主站蜘蛛池模板: 如东县| 苍溪县| 阿拉善左旗| 新竹县| 安化县| 长治县| 宿迁市| 甘洛县| 绿春县| 太仓市| 吉林省| 沙洋县| 遂溪县| 正阳县| 谢通门县| 夏津县| 新宁县| 深圳市| 湖州市| 辽宁省| 榆中县| 弥勒县| 金门县| 大同县| 永善县| 晋江市| 怀仁县| 昭平县| 灵丘县| 东平县| 麻阳| 济宁市| 宾川县| 凉城县| 宝丰县| 绥宁县| 都江堰市| 游戏| 满城县| 塘沽区| 马鞍山市|