泛型课堂练习题

题目要求

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

代码

package generic;

import java.util.ArrayList;
import java.util.Comparator;
/**
 * @Author 雾潋
 * @Version 1.0
 */
public class GenericTest {
    public static void main(String[] args) {
        Employee wulian = new Employee("wulian", 20000, new MyDate(2000, 2, 3));
        Employee zhangsan = new Employee("zhangsan", 1000, new MyDate(2000, 12, 12));
        Employee zhangsan1 = new Employee("zhangsan", 1000, new MyDate(2000, 12, 11));
        Employee lisi = new Employee("lisi", 200, new MyDate(1999, 03, 14));
        ArrayList<Employee> arrayList = new ArrayList<>();
        arrayList.add(wulian);
        arrayList.add(zhangsan);
        arrayList.add(lisi);
        arrayList.add(zhangsan1);
        arrayList.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) { //返回值 int > 0 倒序 int < 0 正序
               if((o1.getName().compareTo(o2.getName()) < 0)) {
                   return -1 ;
               } else if (o1.getName().compareTo(o2.getName()) > 0) {
                  return 1;
               }
               if (o1.getMyDate().getYear() > o1.getMyDate().getYear()){
                   return -1;
               } else if (o1.getMyDate().getYear() < o2.getMyDate().getYear()) {
                   return  1;
               }
               else {
                   if (o1.getMyDate().getMonth() > o1.getMyDate().getMonth()){
                       return -1;
                   }else if (o1.getMyDate().getMonth() < o1.getMyDate().getMonth()) {
                       return 1;
                   }
                   if (o1.getMyDate().getDay() > o1.getMyDate().getDay()){
                       return -1;
                   } else if (o1.getMyDate().getDay() < o1.getMyDate().getDay()) {
                       return 1;
                   }
               }
               return 0;
            }
        });
      //使用for增强遍历
        for(Employee employee: arrayList) {
            System.out.println(employee);
        }
    }
}

class Employee {
    private String name;
    private int sal;
    private MyDate myDate;

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

    public Employee(String name, int sal, MyDate myDate) {
        this.name = name;
        this.sal = sal;
        this.myDate = myDate;
    }

    public String getName() {
        return name;
    }

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

    public int getSal() {
        return sal;
    }

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

    public MyDate getMyDate() {
        return myDate;
    }

    public void setMyDate(MyDate myDate) {
        this.myDate = myDate;
    }
}

class 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 +
                '}';
    }
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值