數(shù)據(jù)加載中……
          CloneTest.java

           

          /**
          *i測(cè)試對(duì)象的克隆及clone方法的重寫(xiě)
          */

          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);
          }

          }

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

          class Student implements Cloneable
          {
           
          private String strName = "";//學(xué)生姓名
           private String strNumber = "";//學(xué)號(hào)
           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 郭興華 閱讀(152) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 鹰潭市| 万山特区| 洪湖市| 班戈县| 桂平市| 饶河县| 南涧| 兴和县| 驻马店市| 台中县| 深泽县| 舟曲县| 吴桥县| 松滋市| 光山县| 通山县| 潜山县| 江安县| 会泽县| 那坡县| 漳平市| 丰宁| 马关县| 潢川县| 成安县| 韩城市| 邻水| 平陆县| 璧山县| 南靖县| 宁晋县| 景德镇市| 黄浦区| 吴旗县| 根河市| 晋江市| 东安县| 永德县| 扶风县| 东乡族自治县| 巨野县|