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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM