import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
private double fen;
Student (String name,int age,double fen)
{
this.name=name;this.age=age;this.fen=fen;
}
public void setName(String name)
{
this.name=name;
}
public void setAge(int age)
{
this.age=age;
}
public void setFen(double fen)
{
this.fen=fen;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getFen()
{
return fen;
}
public int compareTO(Student s)
{
int num=new Integer(this.getAge()).compareTo(new Integer(s.getAge()));
if (num==0)
return this.getName().compareTo(s.getName());
return num;
}
public int hashCode()
{
return name.hashCode()+age*37;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s=(Student)obj;
return this.name.equals( s.name)&&this.age==s.age;
}
}
class Student1
{
public static void main(String[] args)
{
TreeSet<Student> ts=new TreeSet<Student>();//是不是这里出现问题了啊 ??
ts.add(new Student("wangwu",20,78));
ts.add(new Student("zhangsan",20,98));
ts.add(new Student("lisi",20,80));
ts.add(new Student("zhaoliu",20,60));
ts.add(new Student("zhuqi",20,60 ));
Iterator<Student> it=ts.iterator();
while (it.hasNext())
{
Student s=it.next();
System.out.println(s.getName()+" ... "+s.getAge()+"..."+s.getFen());
}
}
}
错误 疑惑求指点
Noname2.java:2: Student 不是抽象的,并且未覆盖 java.lang.Comparable 中的抽象方法
compareTo(Student)
class Student implements Comparable<Student>
^
1 错误
|