超类
package equals;
import java.time.LocalDate;
import java.util.Objects;
public class Employee {
//实例域
private String name;
private double salary;
private LocalDate hireDay;
//构造器
public Employee() {
}
public Employee(String name, double salary, int year, int mouth, int day) {
this.name = name;
this.salary = salary;
this.hireDay = LocalDate.of(year, mouth, day);
}
//get方法
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) {
//1.判断this与otherObject是否引用同一个对象
if (this == otherObject)
return true;
//2.检测otherObject是否为null
if (otherObject == null)
return false;
//3.比较this与otherObject是否属于同一个类(equals语义在每个子类有改变)
//如果子类都有统一的语义可以使用if(!(otherObject instanceof ClassName))
if (getClass() != otherObject.getClass())
return false;
//4.奖otherObject转换为相应的类类型变量
Employee other = (Employee)otherObject;
//5.对所有需要比较的域进行比较,使用==比较基本类型,使用equals比较对象域,Arrays.equals(type[] a, type[] b)比较数组
//Objects.equals(a, b):a和b都为null返回true,其中之一为null返回false;否则返回a.equals(b)
return Objects.equals(name, other.name)
&& salary == other.salary
&& Objects.equals(hireDay, other.hireDay);
}
//返回对象的散列码
public int hashCode() {
//Objects.hash((int|long|short|byte|double|float|char|boolean) value)返回给定值的散列码
//Objects.hash(Object a)如果a为null返回0,否则返回a.hashCode()
//Objects.hash(Object ...objects)返回一个散列码,由提供的所有对象的散列码组合得到
//Objects.hash(type[] a)计算数组a的散列码
return Objects.hash(name, salary, hireDay);
}
//返回表示对象值的字符串,println方法也会直接调用,只要对象与一个字符串通过操作符“+”连接起来就会自动的调用toString方法,即x.toString和“”+x效果一样,一般为:类名+域值
//数组使用Arrays.toString(),多维数组使用Arrays.deepToString()
public String toString() {
//getClass返回包含对象信息的类对象,内容被封装在Class类中。getName返回这个类的名字
return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
}
}
子类
package equals;
import java.util.Objects;
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;
}
//get方法
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
//设置奖金
public void setBonus(double bonus) {
this.bonus = bonus;
}
//重新定义equals方法
public boolean equals(Object otherObject) {
//在子类中重新定义equals,要在其中包含调用super.equals(other)
if (!super.equals(otherObject))
return false;
Manager other = (Manager)otherObject;
return bonus == other.bonus;
}
public int hashCode() {
return super.hashCode() + 17 * new Double(bonus).hashCode();
}
public String toString() {
return super.toString() + "[bonus=" + bonus + "]";
}
}
测试
package equals;
public class test {
public static void main(String[] args) {
Employee Siri1 = new Employee("Siri", 10000, 2001, 9, 7);
Employee Siri2 = Siri1;
Employee Siri3 = new Employee("Siri", 10000, 2001, 9, 7);
Employee Xiaodu = new Employee("Xiaodu", 20000, 2021, 9, 5);
System.out.println("Siri1 == Siri2: " + (Siri1 == Siri2));
System.out.println("Siri1 == Siri3: " + (Siri1 == Siri3));
System.out.println("Siri1.equals(Siri3): " + Siri1.equals(Siri3));
System.out.println("Siri1.equals(Xiaodu): " + Siri1.equals(Xiaodu));
System.out.println("Xiaodu.toString(): " + Xiaodu);
Manager Carl = new Manager("Carl", 15000, 2021, 9, 7);
Manager Boss = new Manager("Boss", 15000, 2021, 9, 7);
Boss.setBonus(5000);
System.out.println("Boss.toString: " + Boss);
System.out.println("Carl.equals(Boss): " + Carl.equals(Boss));
System.out.println("Siri1.hashCode(): " + Siri1.hashCode());
System.out.println("Siri3.hashCode(): " + Siri3.hashCode());
System.out.println("Xiaodu.hashCode(): " + Xiaodu.hashCode());
System.out.println("Carl.hashCode(): " + Carl.hashCode());
}
}
版权声明:本文为shyshyshyshy1原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。