compareTo()方法中的 if(this.getAge()>stu.getAge()) 的this.getAge()指的是?实例化对象传入的参数不都是stu.getAge() 我想了好久都不知道这个this表示什么
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()<stu.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);