Java如何实现深拷贝(2)

@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;
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/bdbbe1f3b1520cde3a76bca829dfadbe.html