Java equals方法与toString方法 初学者笔记

package equals;

public class EqualsTest
{
   public static void main(String[] args)
   {
      var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      var alice2 = alice1;
      var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      var bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

      System.out.println("alice1 == alice2: " + (alice1 == alice2));

      System.out.println("alice1 == alice3: " + (alice1 == alice3));
      //alice1与aclice3是两个对象,储存地址不同

      System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
      //两个对象具有相同的状态(参数相同·,为一个构造器构造等),所以相等

      System.out.println("alice1.equals(bob): " + alice1.equals(bob));
      //不具备相同的状态

      System.out.println("bob.toString(): " + bob);
      //bob引用的是Employee对象,所以此段代码安装Employee类中的toString方法格式打印
      var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      boss.setBonus(5000);//为boss加上奖金
      System.out.println(boss);
      //当boss指向任意对象时,println方法将调用boss.toString()方法并打印得到的字符串结果
      System.out.println("carl.equals(boss): " + carl.equals(boss));
      //经过boss.setBonus(5000);之后boss引用的对象相比car的参数多了一个[bonus=5000.0],所以检测为f
      System.out.println("alice1.hashCode(): " + alice1.hashCode());
      System.out.println("alice3.hashCode(): " + alice3.hashCode());
      //字符串的hashcode是由内容得出的
      System.out.println("bob.hashCode(): " + bob.hashCode());
      System.out.println("carl.hashCode(): " + carl.hashCode());
   }
}

 toString方法使用:

 x.toString()//x本身是或引用一个对象 

 String message = "a"+ x//只要一个对象以+操作符与一个字符串连接起来 //Java编译器就会调用toString方法获得这个对象的字符串描述 

 System.out.println(x)//println方法将调用boss.toString()方法并打印得到的字符串结果 

编写toString时可使用getClass().getName()获取类名

equals方法:

检测一个对象是否等于另一个对象

使用方法:

 Objects.equals(carl,boss));//()中放入比较的对象 

 carl.equals(boss); 

 附上余下两个类:

package equals;

import java.time.*;
import java.util.Objects;

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

   public Employee(String name, double salary, int year, int month, int day)
   {
      this.name = name;
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day);
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public LocalDate getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }

   public boolean equals(Object otherObject)
   {
      //快速测试以查看对象是否相同
      if (this == otherObject) return true;
      //this指代 carl.equals(boss));中的对象cal,otherObject则指代()中的boss
      // 如果显式参数为null,则返回false
      if (otherObject == null) return false;

      // 如果类别不匹配,就不能相等
      if (getClass() != otherObject.getClass()) return false;

      // 现在我们知道otherObject是一个非空员工
      var other = (Employee) otherObject;

      // 测试字段是否具有相同的值
      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 + "]";
   }
}
package equals;

public class Manager extends Employee
{
   private double bonus;

   public Manager(String name, double salary, int year, int month, int day)
   {
      super(name, salary, year, month, day);
      bonus = 0;
   }

   public double getSalary()
   {
      double baseSalary = super.getSalary();
      return baseSalary + bonus;
   }

   public void setBonus(double bonus)
   {
      this.bonus = bonus;//boss的奖金
   }

   public boolean equals(Object otherObject)
   {
      if (!super.equals(otherObject)) return false;
      //先检测与超类是否相等
      var other = (Manager) otherObject;
      // super.equals checked that this and other belong to the same class
      return bonus == other.bonus;
   }

   public int hashCode()
   {
      return java.util.Objects.hash(super.hashCode(), bonus);
   }

   public String toString()
   {
      return super.toString() + "[bonus=" + bonus + "]";
      //子类toString可在调用超类toString的基础上加以补充
   }
}

猜你喜欢

转载自www.cnblogs.com/MR---Zhao/p/12598923.html
今日推荐