java- – Object所有类的超类

  • Post author:
  • Post category:java


Object类 是 java 中所有类的始祖, 在java 中每个类都扩展了 Object

public class Employee {
  private String name;
  private double salary;
  private LocalDate localDate;

  public Employee(String name, double salary, LocalDate localDate) {
    this.name = name;
    this.salary = salary;
    this.localDate = localDate;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public double getSalary() {
    return salary;
  }

  public void setSalary(double salary) {
    this.salary = salary;
  }

  public LocalDate getLocalDate() {
    return localDate;
  }

  public void setLocalDate(LocalDate localDate) {
    this.localDate = localDate;
  }

  @Override

   public boolean equals(Object otherObject) {
      if (this == otherObject) return true;
      if (otherObject == null) return false;
      if (getClass() != otherObject.getClass()) return false;
      var other = (Employee) otherObject;
      return Objects.equals(name, other.name)
         && salary == other.salary && Objects.equals(localDate, other.localDate);
   }

  @Override
  public int hashCode() {
    return Objects.hash(name, salary, localDate);
  }

  @Override
  public String toString() {
    return getClass().getName() + "[ name " + name + " ] [ salary" + salary + "] [ localDate " + localDate + "]";
  }
}



版权声明:本文为zhanglongdream原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。