发布于2021-11-21 21:20 阅读(1181) 评论(0) 点赞(12) 收藏(2)
泛型课堂练习题(韩顺平老师)
定义Employee类
(1)该类包含:private成员变量name,sal,birthday,其中birthday为MyDate类的对象;
(2)为每一个属性定义getter,setter方法;
(3)重写toString方法输出name,sal,birthday
(4)MyDate类包含:private成员变量year,month,year;并为每一个属性定义getter,setter方法;
(5)创建该类的3个对象,并把这些对象放入ArrayList集合中(ArrayList需使用泛型来定义),对集合中的元素进行排序,并遍历输;
排序方式:调用ArrayList的sort方法,传入Comparator对象[使用泛型],先按照name排序,如果name相同,则按生日日期的先后排序。
Employee:
- package ch05;
-
- public 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 + "]";// \n换行
- }
-
-
-
-
-
- }
GenericExercise02:
- package ch05;
-
- import java.util.ArrayList;
- import java.util.Comparator;
-
- @SuppressWarnings({"all"})
- public class GenericExercise02 {
-
- public static void main(String[] args) {
-
- ArrayList<Employee>employees = new ArrayList<>();
- employees.add(new Employee("tom", 20000, new MyDate(2000, 11, 11)));
- employees.add(new Employee("jack", 12000, new MyDate(2001, 12, 12)));
- employees.add(new Employee("hsp", 5000, new MyDate(1980, 10, 10)));
-
- System.out.println("employees=" + 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;
- }
- return emp1.getBirthday().compareTo(emp2.getBirthday());
- }
- });
-
- System.out.println("==对雇员进行排序==");
- System.out.println(employees);
-
-
- }
-
- }
MyDate:
- package ch05;
-
- public 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.getYear();
- if(yearMinus != 0) {
- return yearMinus;
- }
- int monthMinus = month - o.getMonth();
- if(monthMinus != 0) {
- return monthMinus;
- }
- return day - o.getDay();
- }
-
-
- }
原文链接:https://blog.csdn.net/weixin_53173524/article/details/121429994
作者:怎么没有鱼儿上钩呢
链接:http://www.javaheidong.com/blog/article/326702/ce8f32552d3f9d39c40b/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!