1.Employee類的equals
由於題目要求//首先調用父類的equals方法,如果返回true。再比較company與salary。
//比較salary屬性時,使用DecimalFormat df = new DecimalFormat("#.#");保留1位小數
public boolean equals(Object obj) {
if(super.equals(obj)==true) {
Employee other = (Employee)obj;
if(this.company.toString()==null||other.company.toString()==null) {
return false;
}
String df1 = new DecimalFormat("#.#").format(this.salary);
String df2 = new DecimalFormat("#.#").format(other.salary);
if(this.company.toString().compareTo(other.company.toString())==0&&df1.compareTo(df2)==0) {
return true;
}
}
return false;
}
2. 排序比較的方法,非常好用
personList.sort(Comparator.comparing(Person::getName).thenComparing(Person::getAge));
3.判斷是否屬於某個類
if (personList.get(i) instanceof Student)
4。疑惑(・∀・(・∀・(・∀・*)
①為什么引用person的toString卻可以輸出Employee:bo-18-true-IBM-5000.54
public String toString() {
return name+"-"+age+"-"+gender ;
}
解答:這就是覆蓋,這里的person
System.out.println("Student:"+personList.get(i).toString());被student覆蓋,是person類下的student類;
5.錯誤:
使用switch進行識別有許多不清楚之處,以后盡量使用if