class Stucom implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
if(!(s1 instanceof Student))
throw new RuntimeException("不是学生对象");
if(!(s2 instanceof Student))
throw new RuntimeException("不是学生对象");
return s1.getScore().compareTo(s2.getScore ());
//这里如果换成getName,代码打印出来没问题,搞不懂为什么换成成绩就不行了.
}
}
class Student
{
private String name;
private int age;
private int score;
Student(String name,int age,int score)
{
this.name=name;
this.age=age;
this.score=score;
}
public void setShuXing(String n,int b,int c)//对私有变量设置值。
{
this.name=name;
this.age=age;
this.score=score;
}
public String getName( )//私有变量获取值。
{
return name;
}
public int getAge( )
{
return age;
}
public int getScore( )
{
return score;
}
}