1. The display parameter is named Otherobject and later it needs to be converted to another variable called other.
2. Detect if this and otherobject refer to the same object:
if (This==otherobject) return true;
This statement is just an optimization. In fact, this is a frequently used form of amount. Because it is much less expensive to calculate the lighting than to compare the fields in the class one by one.
3. Detects if Otherobject is null, and returns FALSE if null. This test is very necessary.
if (otherobject==null) return false;
4. Compare whether this and otherobject enter the same class. If the semantics of equals are changed in each subclass, use Geiclass to detect
if (GetClass ()!=otherobject.getclass ()) return false;
If all the self-thunder have unified semantics, use instanceof detection
if (! ( Otherobject instanceof ClassName)) return false;
5 converting Otherobject to the appropriate class-type variable
ClassName other = (ClassName) otherobject;
6. Now it's time to compare all the fields that need to be compared. Use = = to compare the base type fields, using equals to compare object fields. Returns True if all the fields match. Otherwise, false is returned.
Return field1==other.field1&& objects.equals (field2,other.field2) &.....;
If you redefine equals in a subclass, you must include the call Super.equals (other)
Tips: For arrays of type fields, you can use the static Arrays.equals method to detect if the corresponding array elements are equal
Proposal to write a perfect Equals method (Java)