compareT方法里面的this.getAge()表达什么?比如b1.compareTo(b2);b1是this.getAge(),b2是stu.getAge(),那么这句代码里面的this.getAge()是什么,主方法只传了一个参数啊???impo...
compareT方法里面的 this.getAge()表达什么?比如 b1.compareTo(b2); b1是this.getAge(),b2是stu.getAge(),那么这句代码里面的this.getAge()是什么,主方法只传了一个参数啊???
import java.util.Arrays;
import java.util.Comparator;
public class Student implements Comparable{
//就简单点按年龄大小排序
private int age;
Student(){
this.age = 0;
}
Student(int age){
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student stu) {
if(this.getAge()>stu.getAge()){
return 1;
}else if(this.getAge()
return -1;
}else{
return 0;
}
}
public static void main(String[] args) {
Student stu[] = new Student[3];
stu[0] = new Student(2);
stu[1] = new Student(1);
stu[2] = new Student(3);
//--------方法一----------
Arrays.sort(stu);
展开