gpt4 book ai didi

java - FunctionalInterface Comparator 有 2 个抽象方法

转载 作者:IT老高 更新时间:2023-10-28 21:14:37 27 4
gpt4 key购买 nike

学习 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com