Boolean Equals (Object x);
This method and the operator "= =" are very different, equals the value of the implementation of the object is logically equal to the judgment, that is, "= =" is simply to determine whether the object's reference is the same.
The equivalence of equals requires the following relationships: reflexive, symmetric, transitive, non-void, and consistent.
Package Equalstesting;public class Person {private string name;private int age;public person (String Name,int age) {This.na me = Name;this.age = age;} public boolean equals (Object x) {if (this = = x) return true;if (x = = null) return False;if (This.getclass ()! = X.getclass ()) re Turn false; person is = (person) x;if (this.name! = that.name) return false;if (this.age! = that.age) return False;return true;} public static void Main (string[] args) {//TODO auto-generated method Stubperson p1 = new Person ("Jay", 30); person P2 = new Person ("Kim", 29); Person P3 = new Person ("Jay", 30); System.out.println (P1.equals (p2)); System.out.println (P2.equals (p1)); System.out.println (P1.equals (p3)); System.out.println (P3.equals (p1)); SYSTEM.OUT.PRINTLN (P1.equals (null));}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java data abstraction equals of 3 methods to be overloaded ()