4.3 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company ,20 分中的一些问题

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