FunctionalInterface Comparator有2个抽象方法 [英] FunctionalInterface Comparator has 2 abstract methods

查看:584
本文介绍了FunctionalInterface Comparator有2个抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Java 8 Lambdas,只是想知道编译器如何知道Comparator中哪个方法用于lambda表达式?
它似乎不是SAM界面?它有2种抽象方法:

Learning Java 8 Lambdas and just wondering how the compiler knows which method in Comparator to use for the lambda expression? It doesn't seem to be a SAM interface? It has 2 abstract methods:

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


推荐答案

equals()不是抽象方法。此方法重写 Object.equals(Object),并且只有Comparator接口才能将javadoc附加到该方法,解释比较器应如何实现 equals()

equals() is not an abstract method. This method overrides Object.equals(Object), and is there only for the Comparator interface to be able to have javadoc attached to the method, explaining how comparators should implement equals().

参见 FunctionalInterface的javadoc


如果是interface声明一个覆盖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 from java.lang.Object or elsewhere.

这篇关于FunctionalInterface Comparator有2个抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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