@Override
publicbooleanequals(Object otherObject) {
if (this == otherObject)
return true;
if (otherObject == null)
return false;
if (!(otherObject instanceof Employee))
return false;
Employee other = (Employee) otherObject;
return name.equals(other.name) && age == other.age && salary.equals(other.salary)
&& hireDay.equals(other.hireDay);
}
@Override
protected Object clone() throws CloneNotSupportedException {
// call Object.clone()
Employee cloned = (Employee) super.clone();
// call mutable fields
cloned.hireDay = (Date) hireDay.clone();
return cloned;
}
}
Output:
true
false
true
--------------------------------------------------------------------------------
// JDK中Date类的克隆方法
public Object clone() {
Date d = null;
try {
d = (Date)super.clone();
if (cdate != null) {
d.cdate = (BaseCalendar.Date) cdate.clone();
}
} catch (CloneNotSupportedException e) {} // Won't happen
return d;
}