美文网首页
继承-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

    Employee/基类 Manager /派生类

  • Java中hashCode和equals方法的比较

    一、equals 首先我们要明白equals和hashcode方法都是从object类中继承过来的。 equals...

  • Java Object类的equals()方法

    所有类都从Object类中继承了equals方法,Object类中equals方法源代码如下: publicboo...

  • kotlin inheritance 继承

    Any 所有类都继承该类,提供默认的三个函数: equals()hashCode()toString()继承声明:...

  • 操作符“==”和equals的用法

    Object:所有类的父类 equals函数被包含在Object中,即所有类都有equals()函数因为继承了 O...

  • Java基础之equals方法

    1、关于equals方法 equals方法是基类Object中的方法,因此对于所有的继承于Object的类都会有该...

  • java——Object类

    Java类继承了Object的所有属性和方法,如: toString(), hashCode(), equals(...

  • Android面试

    JAVA 基础 java的特点 (OOP),封装、继承、多态 ==、equals、hashCode的作用,区别 什...

  • API

    1.Objece类(equals)D15. 1. 接口不继承Objece,只有类继承 引用对象==表示比较对象的内...

  • 3,java list类

    继承体系 优势 List接口在iterator、add、remove、equals和hashCode方法的协定上加...

网友评论

      本文标题:继承-equals

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