美文网首页
继承-equals

继承-equals

作者: Ricky_Zuo | 来源:发表于2019-04-14 16:23 被阅读0次

    Employee/基类

    package equals;
    
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Objects;
    
    public class Employee
    {
       private String name;
       private double salary;
       private Date hireDay;
    
       public Employee(String n, double s, int year, int month, int day)
       {
          name = n;
          salary = s;
          GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
          hireDay = calendar.getTime();
       }
    
       public String getName()
       {
          return name;
       }
    
       public double getSalary()
       {
          return salary;
       }
    
       public Date getHireDay()
       {
          return hireDay;
       }
    
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    
       public boolean equals(Object otherObject)
       {
          // a quick test to see if the objects are identical
          if (this == otherObject) return true;
    
          // must return false if the explicit parameter is null
          if (otherObject == null) return false;
    
          // if the classes don't match, they can't be equal
          if (getClass() != otherObject.getClass()) return false;
    
          // now we know otherObject is a non-null Employee
          Employee other = (Employee) otherObject;
    
          // test whether the fields have identical values
          return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
       }
    
       public int hashCode()
       {
          return Objects.hash(name, salary, hireDay); 
       }
    
       public String toString()
       {
          return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay
                + "]";
       }
    }
    

    Manager /派生类

    package equals;
    
    public class Manager extends Employee
    {
       private double bonus;
    
       public Manager(String n, double s, int year, int month, int day)
       {
          super(n, s, year, month, day);
          bonus = 0;
       }
    
       public double getSalary()
       {
          double baseSalary = super.getSalary();
          return baseSalary + bonus;
       }
    
       public void setBonus(double b)
       {
          bonus = b;
       }
    
       public boolean equals(Object otherObject)
       {
          if (!super.equals(otherObject)) return false;
          Manager other = (Manager) otherObject;
          // super.equals checked that this and other belong to the same class
          return bonus == other.bonus;
       }
    
       public int hashCode()
       {
          return super.hashCode() + 17 * new Double(bonus).hashCode();
       }
    
       public String toString()
       {
          return super.toString() + "[bonus=" + bonus + "]";
       }
    }
    
    

    相关文章

      网友评论

          本文标题:继承-equals

          本文链接:https://www.haomeiwen.com/subject/bplswqtx.html