创建个学生类,内包含学号,年龄,姓名等属性,对属性进行封装

创建构造方法、setter方法、getter方法;
通过自定义比较器类,重写compare()方法,对存入TreeSet集合的学生对象进行排序打印(按照年龄的大小顺序排序;
要求:
存入的学生对象至少为3个;
可先创建学生对象,后 将学生对象放入集合




public class Student implements Comparable{

    public int compareTo(Object obj){
        Student stu = (Student)obj;
        if (this.age>stu.age) return 1;
        return -1;
    }
    public String toString(){
        return this.sId+" "+this.age+" "+this.name;
    }

    int sId;
    int age;
    String name;

    public Student(int sId, int age, String name) {
        this.sId = sId;
        this.age = age;
        this.name = name;
    }

    public Student() {
    }

    public int getsId() {
        return sId;
    }

    public void setsId(int sId) {
        this.sId = sId;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


import java.util.Set;
import java.util.TreeSet;

public class StudentTest {
    public static void main(String[] args) {
        Set set = new TreeSet();
        set.add(new Student(1101,18,"张三"));
        set.add(new Student(1102,19,"李四"));
        set.add(new Student(1103,17,"王五"));

        System.out.println(set);
    }
}