标题: 求教 [打印本页] 作者: 佛山java爱好者 时间: 2014-7-15 11:44 标题: 求教 class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+":"+age;
}
public int hasCode()
{
return name.hasCode()+age*34;
}
public bollean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s=(Student)obj;
return this.name.equals(s.name)&&this.age==s.age;
}
public int compareTo(Student s)
{
int num=new Integer(this.age.compareTo(new Integer(s.age)));
if(num==0)
return this.name.compareTo(s.name);
return num;