这段在学生类中重写的compareTo中的this代表什么?如果o是代表传入的学生对象,那么return中的那个this又代表什么呢?
student中重写的conpareTo:
@Override
public int compareTo(student o) {
// TODO 自动生成的方法存根
return this.id.compareTo(o.id);
}
要求排序的student泛型的list:
List<student> studentlist = new ArrayList<student>();
Random r1 = new Random();
studentlist.add(new student(100000000 + "", "A"));
studentlist.add(new student(r1.nextInt(1000) + "", "MING"));
studentlist.add(new student(r1.nextInt(1000) + "", "Hong"));
studentlist.add(new student(r1.nextInt(1000) + "", "Lv"));
System.out.println("------排序前------");
for (student student : studentlist) {
System.out.println("学生" + student.id + ":" + student.name);
}
Collections.sort(studentlist);
System.out.println("------排序后------");
for (student student : studentlist) {
System.out.println("学生" + student.id + ":" + student.name);
}