Implementing equals method using compareTo
一般问题:当在Java中实现对默认EDCOX1 0Ω方法的重写时,对于简单地利用已经实现的EDCOX1×1的方法与将独立逻辑写入到相等方法中,我应该考虑什么?我注意到有人在另一个问题中提到,
样品
1 2 3 4 5 6 7 8 | @Override public boolean equals(Object obj) { if (obj != null && obj instanceof MyClass) { MyClass msg = (MyClass)obj; return this.compareTo(msg) == 0; } return false; } |
编辑:从可比文件中引用
The natural ordering for a class C is said to be consistent with
equals if and only if e1.compareTo(e2) == 0 has the same boolean value
as e1.equals(e2) for every e1 and e2 of class C. Note that null is not
an instance of any class, and e.compareTo(null) should throw a
NullPointerException even though e.equals(null) returns false
编辑:
在进一步审查后,我发现值得注意的是,可比文件还规定了以下内容:
The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)
因此,由于
除此之外,遵循干燥的原则,如您所建议的,重新使用代码是一个好主意。
根据JavaDoc:
Note that null is not an instance of any class, and e.compareTo(null)
should throw a NullPointerException even though e.equals(null) returns
false.It is strongly recommended, but not strictly required that
(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class
that implements the Comparable interface and violates this condition
should clearly indicate this fact. The recommended language is"Note:
this class has a natural ordering that is inconsistent with equals."
您可以自由地在您的
我认为合同的执行更为重要。