Item 8: Obey the general contract when overriding equals
Equivalence relation(equals必须满足的性质):
Reflexive: For any non-null reference value x, x.equals(x) must return true.
Symmetric: For any non-null reference values x and y, x.equals(y) must return true if and only if y.equals(x) returns true.
Transitive: For any non-null reference values x, y, z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true.
Consistent: For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) must return false.
不满足对称性的例子:
Once you’ve violated the equals contract, you simply don’t know how other objects will behave when confronted with your object.
为了消除该问题,必须避免让CaseInsensitiveString和String进行比较,代码如下:
另一个例子: 现在需要继承这个Point,并加上Color属性如下: 这样写违反了对称性(用一个Point与ColorPoint比较如:Point p = new Point(1, 2); ColorPoint cp = new ColorPoint(1, 2, Color.RED);)。改写equals如下: 这样写避免了Point与ColorPoint比较时造成的对称性违反。但是违反了传递性:事实上,在这种情况下无法实现满足这些性质的equals方法。There is no way to extend aninstantiable class and add a value component while preserving the equalscontract。这里的解决方法是:Item 16.Favorcomposition over inheritance.代码如下:在Java类库中,java.sql.Timestampextends java.util.Date and adds a nanoseconds field.The equals implementationfor Timestamp does violate symmetry.将两者同时用在一个Collection中会导致错误。
recipe for a high-quality equals method:
1. Use the == operator to check if the argument is a reference to this object.
2. Use the instanceof operator to check if the argument has the correct type.
3. Cast the argument to the correct type.
4. For each “significant” field in the class, check if that field of the argument matches the corresponding field of this object.
primitive fields whose type is not float or double : ==
float/double fields:Float.compare/Double.compare(for Float.NaN, -0.0f)
object reference fields : equals
array:Arrays.equals
Some object reference fields may legitimately contain null.To avoid the possibility of a NullPointerException, use this idiom to compare such fields: (field == null ? o.field == null : field.equals(o.field))
5. When you are finished writing your equals method, ask yourself three questions: Is it symmetric? Is it transitive? Is it consistent?
分享到:
相关推荐
Methods.in.Nonlinear.Analysis.Kung-Ching.2005. Methods.in.Nonlinear.Analysis.Kung-Ching.2005.
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;...
|--- Essentials.of.Mathematica.-.With.App.tics.and.Physics.pdf |--- Introduction.to.Maple.for.Physics.St.Maple.Powertools.pdf |--- Introduction.to.Mathematics.With.Maple.pdf |--- Learning.by....
This option will switch from fast-methods to slower methods, but which take less memory. Use 0 (default) for small projects, use 1 for large projects (if ecc32 runs out of memory). 2)....Added: --el_...
mysql-connector-java-5.1.40-bin.jar ognl-3.0.6.jar servlet-api.jar spring-aop-4.0.0.RELEASE.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE....
Good luck to all of you who will take it from here and write some nice applications to use these methods more efficiently we anxiously await your mail. This would be possible with our Mosb3rg and ...
Java How to Program (Late Objects), Tenth Edition is intended for use in the Java programming course. It also serves as a useful reference and self-study tutorial to Java programming. The Deitels’ ...
additional-methods.min.js是微信平台二次开发系统
- Multi-language architecture allows you to use many languages (Pascal, C++, Basic, Java). - Access to any object inside your application (in case you've allowed this). Standard libraries to ...
This book is a first step to addressing these difficulties, providing a broad introduction to the key methods and underlying concepts of mathematical models in ecology and evolution. The book is ...
world guidance you need to solve even the most challenging programming problems and offers an all-new chapter on XML and Java, plus thoroughly revamped coverage of many advanced features—from...
Agile Java™ Development With Spring, Hibernate and Eclipse is a book about robust technologies and effective methods which help bring simplicity back into the world of enterprise Java development....
Nucleus-JosStam Mathematical.Methods.of.Classical.Mechanics.(2nd.Edition)
4.(7.31Card Shuffling and Dealing) Use the methods developed in Exercise 7.30 to write an application that deals two five-card poker hands, evaluates each hand and determines which is better.
commons-codec-1.9.jar commons-logging-1.2.jar fluent-hc-4.5.3.jar httpclient-4.5.3.jar httpclient-cache-4.5.3.jar httpclient-win-4.5.3.jar ...import org.apache.http.client.methods.HttpGet;...
ISO/IEC 7816-2:2007 Identification cards — Integrated circuit cards — Part 2: Cards with contacts — Dimensions and location of the contacts". Iso.org. 2007-10-11. Retrieved 2011-11-12. ISO/IEC 7816...
jquery.validate.methods.js
PNS.BAFS 332.2022_PNS Knapsack Sprayer - Methods of Test (2).pdf
2. Stability Analysis of T-S Fuzzy Systems 3. Extension to Fuzzy Large-Scale Systems 4. Stabilization Methods for T-S Fuzzy Systems 5. Variable Structure Control for T-S Fuzzy Systems 6. Observer-...