【发布时间】:2016-03-25 19:15:37
【问题描述】:
我对 java 有点陌生,对 Collection 框架也很陌生。我知道this指的是当前对象
public class Student implements Comparable <Student> {
String name;
int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public int compareTo(Student s) {
return this.name.compareTo(s.name);
}
public String toString() {
return this.name + ", " + this.grade;
}
}
这里this.name 为空,s.name 确实有一个值,所以我们通过比较this.name.compareTo(s.name); 来尝试做什么
当我们执行Collections.sort(studentList); 时,究竟会发生什么?
代码 sn-p 仅用于演示目的
【问题讨论】:
-
为什么
this.name是空的?如果不知道构造函数是如何被调用的,你就无法知道this.name的内容。
标签: java collections constructor comparator compareto