zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum
    Codeforces Round #412 C. Success Rate (rated, Div. 2, base on VK Cup 2017 Round 3)
    2017 中国大学生程序设计竞赛 女生专场 Building Shops (hdu6024)
    51nod 1084 矩阵取数问题 V2
    Power收集
    红色的幻想乡
    Koishi Loves Segments
    Wood Processing
    整数对
    Room and Moor
  • 原文地址:https://www.cnblogs.com/zlshy/p/11679422.html
Copyright © 2011-2022 走看看