JAVA设计小分队02

import java.util.Objects;

/**
 * @description: 学生类
 * @author: sfj
 * @Email:[email protected]
 * @create: 2023-12-18 20:15
 **/
public class Student implements Comparable<Student> {

    private int sno;
    private String name;
    private int age;
    private int math;
    private int chinese;

    private int english;

    //总分
    private int assemble;


    public Student(int sno, String name, int math, int chinese, int english) {
        this.sno = sno;
        this.name = name;
        this.math = math;
        this.chinese = chinese;
        this.english = english;
    }

    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;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getAssemble(){
        return this.chinese+this.math+this.english;
    }

    @Override
    public String toString() {
        return this.sno+" "+this.name+" "+this.chinese+" "+this.math+" "+this.english+" "+this.getAssemble();
    }

    /**
     * @Description: 按照年龄升序,如年龄相同时,则按姓名length升序
     * @Param: Student
     * @return: 1 0 -1
     * @Author: sfj
     * @Date:
     */
    @Override
    public int compareTo(Student s) {
        if (this.getAssemble() == s.getAssemble()){
            return this.sno-s.sno;
        }else {
            return this.getAssemble()-s.getAssemble()>0?-1:1;
        }
    }


}

import java.util.*;

public class Main {
    public static void main(String[] args) {
        //实例化student对象
        Student s1 = new Student(101,"Zhang",78,87,86);
        Student s2 = new Student(102,"Wang",91,88,90);
        Student s3 = new Student(103,"Li",75,92,84);
        //实例化TreeSet对象
        TreeSet<Student> ts = new TreeSet<>();
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);

        //for循环增强,遍历treeSet集合
        for (Student t : ts) {
            System.out.println(t);
        }
    }
}

你可能感兴趣的:(java)