public static class Student implements Comparable<Student>{
String name;
int chinese;
int math;
int english;
int sum;
public Student(String name,int a,int b,int c){
this.name=name;
this.chinese=a;
this.math=b;
this.english=c;
this.sum=a+b+c;
}
@Override
public String toString() {
String s=name+":"+sum;
return s;
}
@Override
public int compareTo(Student o) {
if(this.sum>o.sum){
return -1;
}else if(this.sum<o.sum){
return 1;
}else{
return this.name.compareTo(o.name);
}
}
代码里面的compareTo方法里面的代码是怎么回事呢,为什么实例化之后放进Set集合就自动按大小拍好序了呢 |
|