当Comparator有两个抽象方法时,它如何成为一个功能界面? [英] How can Comparator be a Functional Interface when it has two abstract methods?
问题描述
在Java 8中,引入了 @FunctionalInterface
注释来表示任何只有一个抽象方法作为功能接口的接口。引入它的原因之一是指示用户(程序员),lambda表达式可以在功能接口的上下文中使用。
In Java 8, the @FunctionalInterface
annotation is introduced to denote any interface that has exactly one abstract method as a functional interface. One of the reason for its introduction is to indicate the user (programmer), that lambda expression can be used in context of a functional interface.
比较器
界面使用 @FunctionalInterface
进行注释。但是,有两种方法是抽象的。
The Comparator
interface is annotated with @FunctionalInterface
. But, two methods are abstract.
int compare(T o1, T o2);
和
boolean equals(Object obj);
在 FunctionalInterface
,明确提到
In the docs of FunctionalInterface
, it is clearly mentioned as
从概念上讲,功能界面只有一种抽象方法。
Conceptually, a functional interface has exactly one abstract method.
是不是等于
方法在这里不被视为抽象?
Isn't the equals
method not considered as abstract here?
推荐答案
文档还说明:
如果接口声明了一个覆盖
java.lang.Object
的公共方法之一的抽象方法,那么这也不会计入接口的抽象方法计数,因为任何实现该接口将具有java.lang.Object
或其他地方的实现。
If an interface declares an abstract method overriding one of the public methods of
java.lang.Object
, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation fromjava.lang.Object
or elsewhere.
由于等于
是其中一种方法,因此界面的抽象方法计数仍为1.
And since equals
is one of those methods, the "abstract method count" of the interface is still 1.
这篇关于当Comparator有两个抽象方法时,它如何成为一个功能界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!