java技術博客

          jsp博客
          數(shù)據(jù)加載中……
          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);
          }

          }

          /*
           * 學生類,包括學生的基本信息,實現(xiàn)了Cloneable接口
           
          */

          class Student implements Cloneable
          {
           
          private String strName = "";//學生姓名
           private String strNumber = "";//學號
           private String strSex = "";//性別
           private String strBirthday = "";//出生年月
           private String strSpeciality = "";//專業(yè)
           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
          +=",專業(yè)="+strSpeciality;
          if(!strAddress.equals(""))
          information
          +=",address="+strAddress;
          return information;
          }

          }

          }

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


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


          網站導航:
           
          主站蜘蛛池模板: 寿光市| 白城市| 正阳县| 亚东县| 濮阳市| 德化县| 东山县| 固阳县| 德州市| 治多县| 贵定县| 沙河市| 大渡口区| 孝义市| 涟水县| 巴林左旗| 新龙县| 泸定县| 徐闻县| 济阳县| 登封市| 高密市| 枣阳市| 天长市| 辽阳市| 印江| 水富县| 义马市| 抚远县| 陕西省| 柳河县| 澄迈县| 克山县| 眉山市| 抚松县| 常熟市| 新蔡县| 达日县| 正蓝旗| 新建县| 潜江市|