今天做了到简单的泛型练习题,自己先敲完实现了,再去看老师的答案,简直羡慕死我,老师的代码看着 “ 太美了 ” 。题目如下:
这是道很简单的练习题,涉及前面所学的ArrayList中sort方法的定制排序,即重写传入的接口的compare方法。下面是我的实现方法:
日期类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 static int compareDate(MyDate date1, MyDate date2) {
if (date1.getYear() == date2.getYear()) {
if (date1.getMonth() == date2.getMonth()) {
if (date1.getDay() > date2.getDay()) {
return 1;
} else {
return -1;
}
} else if (date1.getMonth() > date2.getMonth()) {
return 1;
} else {
return -1;
}
} else if (date1.getYear() > date2.getYear()) {
return 1;
} else {
return -1;
}
}
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;
}
}
员工类Employee:
class Employee {
private String name;
private double sal;
private MyDate birthday;
@SuppressWarnings({"all"})
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 name + " " + sal + " " + birthday.getYear() + "-"
+ birthday.getMonth() + "-" + birthday.getDay();
}
}
测试类Demo:
public class Demo {
@SuppressWarnings({"all"})
public static void main(String[] args) {
ArrayList emlist = new ArrayList<>();
Employee em1 = new Employee("JJLin", 34000, new MyDate(1989, 3, 23));
Employee em2 = new Employee("GEA", 24005, new MyDate(1995, 4, 12));
Employee em3 = new Employee("Jack", 45098, new MyDate(1994, 12, 10));
emlist.add(em1);
emlist.add(em2);
emlist.add(em3);
System.out.println(emlist);
emlist.sort(new Comparator() {
@Override
public int compare(Employee o1, Employee o2) {
if (o1.getName().compareTo(o2.getName()) > 0) {
return 1;
} else if (o1.getName().compareTo(o2.getName()) < 0) {
return -1;
} else {
return MyDate.compareDate(o1.getBirthday(), o2.getBirthday());
}
}
});
System.out.println(emlist);
}
}
我将日期的比较封装成一个MyDate类中的方法,这样看也没错,功能也确实能实现,但缺点很明显:代码冗余度较高、可读性不够。下面是老师对于日期比较的代码:
class MyDate implements Comparable{//老师实现了Comparable接口 private int year; private int month; private int day; //该类除MyDate实现了Comparable接口、原先的 //compareDate方法替换成compareTo, 其余代码不变 @Override public int compareTo(MyDate date) { int yearMinus = this.year - date.year; if (yearMinus != 0){ return yearMinus; } int monthMinus = this.year - date.year; if (monthMinus != 0) { return monthMinus; } int dayMinus = this.day - date.day; if (dayMinus != 0) { return dayMinus; } return 0; } }
main方法中,只看定制排序部分,其余不变:
emlist.sort(new Comparator() { @Override public int compare(Employee o1, Employee o2) { int nameres = o1.getName().compareTo(o2.getName()); if (nameres != 0){ return nameres; } return o1.getBirthday().compareTo(o2.getBirthday()); } });
自己对日期判断用了 if - else 分支结构,但没用好,反而让代码量增加,阅读体验感不佳。老师的代码值得学习的地方:
1. 利用好比较值的差值,作返回值,减少代码量,逻辑更清晰;
2. 让日期类实现Comparable接口,使后续有日期比较,都可用重写的该方法;
在这记录下,方便日后复习



