public boolean equals(Object otherObject) {

if (this == otherObject)
return true;

if (otherObject == null) 
return false;

if (getClass() != otherObject.getClass())
return false;

Employee other = (Employee) otherObject;
return name.equals(other.name) 
&& salary == other.salary
&& hireDay.equals(other.hireDay);
}
我觉得吧。第一个if完全可以判断出来,直接加一个else 反回false不就可以了吗?为什么加后面的两个if 和一个return呢?

解决方案 »

  1.   

    比如说:otherobject = 苹果,如果this不是苹果,那么他可能有2种情况,一个是空,另一个可能就是橘子、香蕉了(也不等于苹果),因此除了if (this == otherObject) 还有if (getClass() != otherObject.getClass()) 。
      

  2.   

    原来这题是判断的不是这个otherobject是苹果..需是判断的name.equals(other.name) 
    && salary == other.salary 
    && hireDay.equals(other.hireDay); 多谢,我已经明白了分不多请多包涵