zxy0307's picture
Upload 2300 files
0284885 verified
Raw History Blame Contribute Delete
1.22 kB
package TreeSet_package1;
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
//this:表示当前要添加的元素
//o:表示已经在红黑树存在的元素
//返回值:
//负数:表示当前要添加的元素是小的,存左边
//正数:表示当前要添加的元素是大的,存右边
//0:表示当前要添加的元素已经存在,舍弃
public int compareTo(Student o) {
//指定排序的规则
//只看年龄,我想要按照年龄的升序进行排序
int result = this.getAge() - o.getAge();
return result;
}
}