JAVA设计小分队02

import java.util.Objects;

/**
 * @description: 学生类
 * @author: sfj
 * @Email:2238414084@qq.com
 * @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);
        }
    }
}
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值