equals() method is one of the basic methods implemented in the Object base class and is used to customize the equality rules of objects.
The equals() method has been defined in Object, but this method is directly implemented using the == operator. Therefore, if the subclass does not override this method, then the subclass object will use Object when comparing. The result of equals() defined in is the same as the comparison result of the == operator.
#Object class equals() method inpublic boolean equals(Object obj) {return (this == obj); }
public boolean equals(Object obj) {if (this == obj) return true;if(obj != null && obj.getClass() == Person.class) {// 此处,使用 obj.getClass() == Person.class 来确定类型相同时,才进行判等Person p = (Person) obj;// 接下来是内容判断// ...} }
The above is the detailed content of Override equals method. For more information, please follow other related articles on the PHP Chinese website!