jjzjj

java - FunctionalInterface Comparator 有 2 个抽象方法

coder 2023-05-16 原文

学习 Java 8 Lambda,只是想知道编译器如何知道 Comparator 中的哪个方法用于 lambda 表达式? 好像不是SAM接口(interface)?它有 2 个抽象方法:

@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
    boolean equals(Object obj);
}

最佳答案

equals() 不是抽象方法。这个方法覆盖了Object.equals(Object),并且只有Comparator接口(interface)能够将javadoc附加到方法上,解释了比较器应该如何实现equals().

javadoc of FunctionalInterface :

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 from java.lang.Object or elsewhere.

关于java - FunctionalInterface Comparator 有 2 个抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23721759/

有关java - FunctionalInterface Comparator 有 2 个抽象方法的更多相关文章

随机推荐