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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 江安县| 吉水县| 紫阳县| 许昌市| 秦安县| 牟定县| 莲花县| 漳平市| 蛟河市| 错那县| 绍兴县| 弥勒县| 肇源县| 甘孜县| 鹿泉市| 哈尔滨市| 金阳县| 句容市| 抚州市| 太和县| 溧水县| 虹口区| 宣武区| 嘉义县| 得荣县| 浮梁县| 泽州县| 澜沧| 大竹县| 绥中县| 台江县| 东平县| 北辰区| 枝江市| 房产| 射阳县| 小金县| 高阳县| 北票市| 滕州市| 天峨县|