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 郭兴华 阅读(119) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: