假设我们有一个 Employee 类,它具有以下数据字段和函数。该程序试图通过比较它们的和来查看 2Employees
是否相等name
address
public class Employee{
private String name;
private double hours;
private double rate;
private Address address;
@Override
public boolean equals(Object obj){
if(obj == this) return true;
if(obj == null) return false;
if(this.getClass() == obj.getClass()){
Employee other = (Employee) obj;
return name.equals(other.name) && address.equals(other.address);
}else{
return false;
}
}
为什么我们不这样做public boolean equals(Employee obj)
(不同的参数)?