泛型练习题

/**
 * 定义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相同,则按生日日期的先后排序。【即:定制排序】
 * 有一定难度,比较经典泛型使用案例
 */
public class Exercise03 {
    public static void main(String[] args) {
        /**
         * 定义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相同,则按生日日期的先后排序。【即:定制排序】
         * 有一定难度,比较经典泛型使用案例
         */
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("tom",5000,new MyDate(2000,11,1)));
        employees.add(new Employee("jak",3000,new MyDate(2001,12,5)));
        employees.add(new Employee("gy",7000,new MyDate(1998,10,1)));
        System.out.println(employees);

        System.out.println("====对雇员进行排序====");

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                //先按照name排序,如果name相同,则按生日日期的先后排序。即:定制排序
                if (!(o1 instanceof Employee && o2 instanceof Employee)){
                    System.out.println("类型不正确");
                    return 0;
                }
                //比较name
                int i = o1.getName().compareTo(o2.getName());
                if (i!=0) return i;

                //下面是对 birthday 的比较,因此,我们把这个比较放在MyDate类完成
                //封装后,可维护性和复用性,就大大增强
                return o1.getBirthday().compareTo(o2.getBirthday());
            }
        });
        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 "\nEmployee{" +
                "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) { //把对birthday 比较放在这里

        int yearMinus = year - o.getYear();
        if(yearMinus !=0) {
            return yearMinus;
        }
        //如果year相同,就比较month
        int monthMinus = month - o.getMonth();
        if(monthMinus != 0) {
            return monthMinus;
        }
       //如果year和 month相同,比较day
        return day - o.getDay();

    }
}

输出:

[
Employee{name='tom', sal=5000.0, birthday=MyDate{year=2000, month=11, day=1}},
Employee{name='jak', sal=3000.0, birthday=MyDate{year=2001, month=12, day=5}},
Employee{name='gy', sal=7000.0, birthday=MyDate{year=1998, month=10, day=1}}]
====对雇员进行排序====
[
Employee{name='gy', sal=7000.0, birthday=MyDate{year=1998, month=10, day=1}},
Employee{name='jak', sal=3000.0, birthday=MyDate{year=2001, month=12, day=5}},
Employee{name='tom', sal=5000.0, birthday=MyDate{year=2000, month=11, day=1}}]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值