1. equals是类的实例方法。
2.方法翻译
public boolean equals(Object obj)
(句子结构:动宾,宾语由whether引导的名词从句充当。some other + 可数名词单数,译为某一事物。)
指示了某一对象与这个对象是否相等。
The equals
method implements an equivalence relation on non-null object references:
equals方法实现了关于非空对象引用的一种等价关系。
- It is reflexive: for any non-null reference value
x
,x.equals(x)
should returntrue
. - 它是反身的:对于任何非空引用值x, x.equals(x)应该返回ture。
- It is symmetric: for any non-null reference values
x
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - 它是对称的: 对于任何非空引用值x和y, 当且仅当y.equals(x)返回true时,x.equals(y)应该返回true。
- It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - 它是传递的: 对于任何非空引用值x,y,和z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)应该返回true。
- It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - 它是一致的:对于任何非空引用值x和y,x.equals(y)的多次调用总是返回true或总是返回false,不提供修改使用于对象的equals比较的信息。
- For any non-null reference value
x
,x.equals(null)
should returnfalse
. - 对于任何非空引用值x,x.equals(null)应该返回false。
The equals
method for class Object
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x
and y
, this method returns true
if and only if x
and y
refer to the same object (x == y
has the value true
).
类Object的equlas方法,实现了关于对象的最有区别的可能等价关系;即,对于任何非空引用值x和y,当且仅当x和y引用同一个对象时(x == y得到值true),这个方法返回true。
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the general contract for the hashCode
method, which states that equal objects must have equal hash codes.
注意,无论何时这个方法被重写,一般需要重写hashCode方法,为了保持与hashCode方法通用一致,这样做就规定了相等的对象必须有相等的散列码。
-
Parameters: 形参
-
obj
- the reference object with which to compare.参与比较的引用对象。
Returns: 返回
-
true
if this object is the same as the obj argument;false
otherwise.如果这个对象与obj参数相同返回true,否则返回false。