When overwriting Equb, follow the general conventions:
- The reflexive nature. For any non-null reference value X,xequals (x) must return True.
- Symmetry. For any non-null reference value x and Y, x.equals (y) must return True when the Y.equals (x) of the document returns True.
- Transitivity. For any non-null reference value x, Y and Z, if X.equals (y), and Y.equals (z), then X.equals (z) is true.
- Consistency. For non-null reference values x and Y, multiple calls to X.equals (y) return true, or always return false, as long as the information used by the comparison operation on the object is not modified.
- Non-nullability. X,x.equals (NULL) must be false for any non-null reference value.
Tips for achieving high-quality equal methods:
- Use the = = operator to check if the parameter is a reference to this object.
- Use the instanceof operator to check if the parameter is the correct type.
- Convert the parameters to the correct type.
- For each key field in the class, check to see if the field in the parameter matches the corresponding field in the object.
- Determine the symmetry, consistency, and transitivity of the Equals method.
- overriding equals always overwrites hashcode.
- Don't try to make equals too smart.
- Do not replace object objects in the equals declaration with other types.
Effactive Java-A common method for all objects