练习——泛型小练

package com.generic;

import java.util.ArrayList;
import java.util.Comparator;

/*
定义Employee类
1)该类包含:private成员变量name,sal,birthday,其中 birthday为 MyDate类的对象;
2)为每一个属性定义 getter, setter方法;
3)重写toString方法输出name, sal, birthday
4) MyDate类包含:private成员变量month,day,year; 并为每一个属性定义 getter,setter方法;
5)创建该类的3个对象,并把这些对象放入ArrayList 集合中 (ArrayList需使用泛型来定义),,对集合中的元素进行排序,并遍历输出:
排序方式:调用ArrayList的 sort方法,传入 Comparator对象[使用泛型],先按照name排序,如果name相同,则按生日日期的先后排序。【即:定制排序】

 */

@SuppressWarnings({"all"})
public class GenericExercise {
    public static void main(String[] args) {

        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("tom", 10000,new MyDate(2000,1,3)));
        employees.add(new Employee("qiu", 12000,new MyDate(2001,2,7)));
        employees.add(new Employee("jack", 16000,new MyDate(1999,5,3)));
        employees.add(new Employee("jack", 16000,new MyDate(1939,5,3)));
        employees.add(new Employee("jack", 16000,new MyDate(1939,2,3)));
        employees.add(new Employee("jack", 16000,new MyDate(1939,2,1)));
        System.out.println(employees);


        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee emp1, Employee emp2) {
                if (!(emp1 instanceof Employee && emp2 instanceof Employee)){
                    System.out.println("===输入数据格式不正确===");
                    return 0;
                }
                int i = emp1.getName().compareTo(emp2.getName());
                if (i != 0){
                    return i;
                }
                /*
                int yearMinus = emp1.getBirthday().getYear() - emp2.getBirthday().getYear();
                if (yearMinus != 0){
                    return yearMinus;
                }
                int monthMinus = emp1.getBirthday().getMonth() - emp2.getBirthday().getMonth();
                if (monthMinus != 0){
                    return monthMinus;
                }
                return emp1.getBirthday().getDay() - emp2.getBirthday().getDay();
                */

                //封装一下MyDate的比较操作
                int comparea01 = emp1.getBirthday().compareTo(emp2.getBirthday());

                return comparea01;


            }
        });
        System.out.println("===排序后结果===");
        System.out.println(employees);


    }
}

class Employee{
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

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

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "\n" + "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

class MyDate implements Comparable<MyDate>{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(MyDate o) {
        int yearMinus = year - o.year;
        if (yearMinus != 0){
            return yearMinus;
        }
        int monthMinus = month - o.month;
        if (monthMinus != 0){
            return monthMinus;
        }
        return day - o.day;
    }
}
posted @   Q1uuuu  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示