在函数接口eg- toString,equals中继承对象类方法有什么用? [英] What is the use of inheriting object class methods in functional interface eg- toString, equals
问题描述
我找到了以下代码,继承的equals()和toString()方法的用途是什么。
I found following code, What is use of inherited equals() and toString() method.
@FunctionalInterface
public interface FunInterface<T> {
// An abstract method declared in the functional interface
int test(T o1, T o2);
// Re-declaration of the equals() method in the Object class
boolean equals(Object obj);
String toString();
}
推荐答案
(re)的主要原因)声明这样一种方法,就是扩展和记录合同。如果是 Comparator.equals(...)
,这并不是那么明显,因为它没有指定那么多新闻。它说
The main reason to (re)declare such a method, is to extend and document the contract. In case of Comparator.equals(…)
, it’s not that obvious, as it doesn’t specify so much news. It says
此方法必须遵守
Object.equals(Object)
。此外,如果指定的对象也是比较器,并且它与此比较器的顺序相同,则此方法可以返回true
。因此,comp1.equals(comp2)
意味着sgn(comp1.compare(o1,o2))== sgn(comp2.compare(o1, o2))
为每个对象引用o1
和o2
。
This method must obey the general contract of
Object.equals(Object)
. Additionally, this method can returntrue
only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus,comp1.equals(comp2)
implies thatsgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2))
for every object referenceo1
ando2
.
无论如何大多数人都会假设。更糟糕的是,没有覆盖等于
是好的,如果不是说执行此合同的最佳方式,特别是因为JRE类从不考虑比较器的平等。
which most people would have assumed anyway. Even worse, not overriding equals
is fine, if not to say the best way to enforce this contract, especially as the JRE classes never consider comparator equality anyway.
要获得更好的覆盖方法示例,请考虑
To get better examples of overriding methods, consider
返回
true
当且仅当指定的对象也是列表时,两个列表都具有相同的大小,两个列表中所有相应的元素对都是等于
。 (如果Objects.equals(e1,e2)<,则两个元素
e1
和e2
相等/ code>。)
Returns
true
if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists areequal
. (Two elementse1
ande2
are equal ifObjects.equals(e1, e2)
.)
...
List.hashCode()
返回此列表的哈希码值。列表的哈希码被定义为以下计算的结果:
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1;
for (E e : list)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
这确保 list1.equals(list2)
暗示任何两个列表的 list1.hashCode()== list2.hashCode()
, list1
和 list2
,根据 Object.hashCode()
的一般合约的要求。
This ensures that list1.equals(list2)
implies that list1.hashCode()==list2.hashCode()
for any two lists, list1
and list2
, as required by the general contract of Object.hashCode()
.
这提供了一个扩展的合同,只需使用 equals(对象)
和就无法实现hashCode()
从 java.lang.Object继承的方法
。不幸的是,接口不能强制执行其实现类来覆盖这些方法,但这不应该阻止它声明它来记录合同。
This provides an extended contract that can not be fulfilled by simply using the equals(Object)
and hashCode()
methods inherited from java.lang.Object
. Unfortunately, an interface can not enforce its implementation classes to override these methods, but that shouldn’t stop it from declaring it to document the contract.
当然,拥有这样的合同与将接口用作功能接口的意图不兼容,因为lambda表达式和方法引用不能覆盖从<$ c继承的方法$ c> java.lang.Object 提供更具体的实现。
Of course, having such a contract would not be compatible with an intention of using an interface as functional interface, as lambda expressions and method references can not override methods inherited from java.lang.Object
to provide more specific implementations.
但 java.util.Comparator 早在Java 8之前就已经在Java 2中引入了code>,它引入了功能接口的概念。如上所述,它的特殊之处在于我们仍然可以使用
是合适的。 Comparator
作为功能接口,因为等于
实现继承自<$关于为 java.util.Comparator.equals
指定的合约,c $ c> java.lang.Object
But java.util.Comparator
was introduced in Java 2, long before Java 8, which introduced the concept of functional interfaces. As said, it’s special in that we still can use Comparator
as functional interface, as the equals
implementation inherited from java.lang.Object
is fine regarding the contract specified for java.util.Comparator.equals
.
因此,在设计用作功能接口的新接口时,不应声明与 java.lang.Object的接口匹配的方法
。
So when designing new interfaces intended to be used as functional interface, you should not declare methods matching those of java.lang.Object
.
这篇关于在函数接口eg- toString,equals中继承对象类方法有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!