Equals method achieves equivalent relationship, usually meet the following conditions
REFLEXIVE
Symmetric
Transit
Consistent
For any non-air reference value x, x.equals (null) must return false.
To achieve high quality Equals methods, you can do it as follows:
Use the == operator to check "A reference to whether the argument is pointing to the object", if yes, return true;
Use the InstanceOf operator to check "whether the argument is the right type",
If not, return false;
Convert the real parameters to the correct type
For each "Signific" domain in this class, check whether the domains in the actors are matched to the corresponding domain values in the current object.
Returns true if all tests are successful;
E.g:
Class Programer {Int Number; Public Program (INT N) {Number = n;} // must overwrite Hashcode () method () method () {Return Number;} public boolean equals (object obj) {if (this == Obj )) RETURN TRUE; // Compares whether the reference of the two objects is equal to if (OBJ == null) return false; // checks OBJ) whether it is empty if (! (Obj InstanceOf (Program))) Return False; Return (programer == ((programer) .Number);}}
When the Equals method is written, it should be satisfied: Is it symmetrical, passed, consistent? Note:
1) When you rewrite Equals, you always have to change Hascode Method 2) Do not replace the Object object in the equals declaration to other types.
When your Equals can't work properly, see if it is a mistake to make the following example.
Public Boolean Equals (MyClass O) {
...
} This method is not an equals method for overlying the Object class.