程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Java_泛型练习题

发布于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:

  1. package ch05;
  2. public class Employee {
  3. private String name;
  4. private double sal;//薪水
  5. private MyDate birthday;
  6. public Employee(String name, double sal, MyDate birthday) {
  7. this.name = name;
  8. this.sal = sal;
  9. this.birthday = birthday;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public double getSal() {
  18. return sal;
  19. }
  20. public void setSal(double sal) {
  21. this.sal = sal;
  22. }
  23. public MyDate getBirthday() {
  24. return birthday;
  25. }
  26. public void setBirthday(MyDate birthday) {
  27. this.birthday = birthday;
  28. }
  29. @Override
  30. public String toString() {
  31. return "\nEmployee [name=" + name + ", sal=" + sal + ", birthday=" + birthday + "]";// \n换行
  32. }
  33. }

 GenericExercise02:

  1. package ch05;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. @SuppressWarnings({"all"})
  5. public class GenericExercise02 {
  6. public static void main(String[] args) {
  7. ArrayList<Employee>employees = new ArrayList<>();
  8. employees.add(new Employee("tom", 20000, new MyDate(2000, 11, 11)));
  9. employees.add(new Employee("jack", 12000, new MyDate(2001, 12, 12)));
  10. employees.add(new Employee("hsp", 5000, new MyDate(1980, 10, 10)));
  11. System.out.println("employees=" + employees);
  12. employees.sort(new Comparator<Employee>() {
  13. @Override
  14. public int compare(Employee emp1,Employee emp2) {
  15. if(!(emp1 instanceof Employee && emp2 instanceof Employee)) {
  16. System.out.println("类型不正确...");
  17. return 0;
  18. }
  19. int i = emp1.getName().compareTo(emp2.getName());
  20. if(i != 0) {
  21. return i;
  22. }
  23. return emp1.getBirthday().compareTo(emp2.getBirthday());
  24. }
  25. });
  26. System.out.println("==对雇员进行排序==");
  27. System.out.println(employees);
  28. }
  29. }

MyDate:

  1. package ch05;
  2. public class MyDate implements Comparable<MyDate>{
  3. private int year;
  4. private int month;
  5. private int day;
  6. public MyDate(int year, int month, int day) {
  7. this.year = year;
  8. this.month = month;
  9. this.day = day;
  10. }
  11. public int getYear() {
  12. return year;
  13. }
  14. public void setYear(int year) {
  15. this.year = year;
  16. }
  17. public int getMonth() {
  18. return month;
  19. }
  20. public void setMonth(int month) {
  21. this.month = month;
  22. }
  23. public int getDay() {
  24. return day;
  25. }
  26. public void setDay(int day) {
  27. this.day = day;
  28. }
  29. @Override
  30. public String toString() {
  31. return "MyDate [year=" + year + ", month=" + month + ", day=" + day + "]";
  32. }
  33. @Override
  34. public int compareTo(MyDate o) {
  35. int yearMinus = year - o.getYear();
  36. if(yearMinus != 0) {
  37. return yearMinus;
  38. }
  39. int monthMinus = month - o.getMonth();
  40. if(monthMinus != 0) {
  41. return monthMinus;
  42. }
  43. return day - o.getDay();
  44. }
  45. }

原文链接:https://blog.csdn.net/weixin_53173524/article/details/121429994



所属网站分类: 技术文章 > 博客

作者:怎么没有鱼儿上钩呢

链接:http://www.javaheidong.com/blog/article/326702/ce8f32552d3f9d39c40b/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)