是否有任何设计模式(或惯用语)有助于在Java中实现equals()方法?这项任务并不困难,但是在大多数情况下,它几乎是相同的。这就是为什么我想有一个模式但我没有找到它的原因。

UPDATE


我选择了方法:generate equals() method in Eclipse,但是...(在AbstractList中)找到了一种更好的方法来使生成的代码更好:

if (!(attributes ==null ? other.attributes==null : attributes.equals(other.attributes)))
    return false;


而不是生成:

if (attributes == null) {
        if (other.attributes != null)
        return false;
    } else if (!attributes.equals(other.attributes))
        return false;

最佳答案

通常,要实现equals()方法,我要做的是:我从Eclipse生成那些,因为Eclipse可以很好地生成hashCode,toString和equals()方法。

关于java - 实现equals()方法设计模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12833654/

10-13 01:21