在函数接口eg- toString,equals中继承对象类方法有什么用? [英] What is the use of inheriting object class methods in functional interface eg- toString, equals

查看:288
本文介绍了在函数接口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 return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.

无论如何大多数人都会假设。更糟糕的是,没有覆盖等于是好的,如果不是说执行此合同的最佳方式,特别是因为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 are equal. (Two elements e1 and e2 are equal if Objects.equals(e1, e2).)

...



List.hashCode()

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 ,它引入了功能接口的概念。如上所述,它的特殊之处在于我们仍然可以使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆