java技術博客

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

           

          /**
          *通過這個程序,我們要測試接口的多重實現,并學習對象比較方法的實現
          */

          import java.util.Arrays
          public class MuilInterfaceTest{
          public static void main(String[] args){
          Student[] staff
          =new student[3];
          staff[
          0]=new Student("tom","20031020");
          staff[
          1]=new Student("jack","20031022");
          staff[
          2]=new Student("rose","20021023");
          Arrays.sort(staff);
          for(int i=0;i<staff.length;i++)
          {
          System.out.println((Student)staff[i]);
          }
          }
           }


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

          class Student implements Person, Comparable
          {
           
          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 int compareTo(Object otherObject){
          Student other
          =(Student)otherObject;
          int otherNumber=Integer.parseInt(other.strNumber);
          int thisNumber=Integer.parseInt(this.strNumber);
          if(thisNumber>otherNumber)
          return 1;
          else if(thisNubmer==otherNumber)
          return 0;
          else return -1;
          }



          public String getName()
           
          {
            
          return strName;
           }


           
          public String getStudentNumber()
           
          {
            
          return strNumber;
           }


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


           
          public String getSex()
           
          {
            
          return strSex;
           }


           
          public String getBirthday()
            
          {
            
          return strBirthday;
           }


           
          public void setStudentBirthday(String birthday)
           
          {
            strBirthday 
          = birthday;
           }


           
          public String getStudentSpeciality()
           
          {
            
          return strSpeciality;
           }


           
          public void setStudentSpeciality(String speciality)
           
          {
            strSpeciality 
          = speciality;
           }


           
          public String getAddress()
           
          {
            
          return strAddress;
           }


           
          public void setAddress(String address)
           
          {
            strAddress 
          = address;
           }

          public String toString(){
          String information
          ="student name="+strName+"student numbeer="+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 15:02 郭興華 閱讀(125) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 罗城| 德格县| 夏津县| 防城港市| 武强县| 融水| 巴青县| 徐州市| 津市市| 通州区| 息烽县| 福州市| 安乡县| 子长县| 扎赉特旗| 定西市| 武冈市| 宜州市| 沧州市| 南和县| 林西县| 天全县| 余干县| 特克斯县| 东乌珠穆沁旗| 德兴市| 玛多县| 峨眉山市| 长沙市| 贵港市| 扎兰屯市| 保德县| 屏东市| 孟州市| 通道| 江华| 屯门区| 彭阳县| 绥滨县| 嵊泗县| 湘潭县|