本文介绍了应该 'Comparable<T>'是一个“功能接口"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数式接口的定义是函数式接口是只有一个抽象方法的接口(除了 Object 的方法),因此代表一个单一的函数契约."

The definition of a functional interface is "A functional interface is an interface that has just one abstract method(aside from the methods of Object ), and thus represents a single function contract."

根据这个定义,Comparable绝对是一个函数式接口.

According to this definition, the Comparable<T> is definitely a functional interface.

lambda 表达式的定义是一个 lambda 表达式就像一个方法:它提供了一个形式参数列表和一个主体——一个表达式或块——用这些参数表示."

The definition of a lambda expression is "A lambda expression is like a method: it provides a list of formal parametersand a body - an expression or block - expressed in terms of those parameters."

对 lambda 表达式的求值会生成一个函数式接口的实例.

Evaluation of a lambda expression produces an instance of a functional interface.

因此,lambda 表达式的目的是能够通过实现来创建函数式接口的实例功能接口的单一功能.IE.允许使用单个函数创建实例.

Thus, the purpose of the lambda expression is to be able to create an instance of the functional interface, by implementingthe single function of the functional interface. ie. to allow the creation of an instance with the single function.

我们来看看Comparable,这个接口是设计成单个函数使用的吗?IE.它是专为创建具有这个单一功能的实例而设计的吗?

Let us look at Comparable<T>, is this interface designed for use as a single function?ie. was it designed for the creation of instances with this single function only?

Comparable 的文档以此接口对每个类的对象强加了总排序实施它.这种排序称为类的自然排序,类的 compareTo 方法称为作为其自然的比较方法."

The documentation of Comparable<T> starts with "This interface imposes a total ordering on the objects of each class thatimplements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referredto as its natural comparison method."

上面的句子清楚地表明 Comparable 不是设计为用作单个函数,而是始终意在通过添加此单个函数由类实现,该类对其实例具有自然顺序.

The above sentence makes it clear that the Comparable<T> is not designed to be used as a single function, but is alwaysmeant to be implemented by a class, which has natural ordering for its instances, by adding this single function.

这意味着它不是设计为使用 lambda 表达式创建的?

Which would mean that it is not designed to be created by using a lambda expression?

关键是我们不会有任何仅可比较的对象,它旨在实现并因此使用作为类的附加功能.

The point is that we would not have any object which is just Comparable only, it is meant to be implemented and thus usedas an additional function for a class.

那么,Java 语言中是否有一种方法可以阻止为 Comparable 创建 lambda 表达式?接口的设计者能否决定这个接口是由一个类来实现的,而不是由一个类来实现的?使用 lambda 表达式使用此单一方法创建为实例?

So, is there a way in the Java language, by which creation of a lambda expression for Comparable<T> is prevented?Can the designer of an interface decide that this interface is meant to be implemented by a class and not meant to becreated as an instance with this single method by use of a lambda expression?

仅仅因为一个接口碰巧只有一个抽象方法,它不应该被认为是一个函数式接口.

Simply because an interface happens to have a single abstract method, it should not be considered as a functional interface.

也许,如果Java提供了像NotFunctional这样的注解,编译器就可以检测到这个接口没有被使用用于创建 lambda 表达式,例如.

Maybe, if Java provides an annotation like NotFunctional, it can be checked by the compiler that this interface is not usedfor the creation of a lambda expression, eg.

@NotFunctional
public interface Comparable<T> { public int compareTo(T t); }

推荐答案

在需要具有单个抽象方法的接口实例的情况下,可以使用 lambda 表达式.你写道,

A lambda expression can be used where an instance of an interface with a single abstract method is required. You wrote,

仅仅因为一个接口恰好只有一个抽象方法,它不应该被认为是一个函数式接口.

这是完全正确的.拥有一个抽象方法是接口的结构属性,这使得它可以用 lambda 实现.然而,一个接口是否有意义语义适合用 lambda 实现是另一回事.后者是 @FunctionalInterface 注释的目的.当它出现在接口上时,它表明意图该接口对于用 lambda 实现是有用的.

This is exactly correct. Having a single abstract method is a structural property of an interface, one that makes it eligible to be implemented with a lambda. However, whether an interface makes sense or is semantically sensible to be implemented with lambda is a different story. The latter is the purpose of the @FunctionalInterface annotation. When it is present on an interface, it indicates the intent that the interface is useful to be implemented with a lambda.

值得注意的是,Comparable 接口缺少 @FunctionalInterface 注释.

Notably, the Comparable interface lacks the @FunctionalInterface annotation.

虽然将 lambda 用作 Comparable 实现可能是荒谬的,但似乎没有任何理由创建一种机制来阻止这种情况的发生.这样做似乎不会成为错误的来源,这将是开发这种机制的一个很好的理由.相比之下,@FunctionalInterface 注释旨在引导程序员朝着正确方向发展,而不是禁止某些可以说是错误但似乎并不真正有害的东西.

While it's probably nonsensical to use a lambda as a Comparable implementation, there doesn't seem to be any reason to create a mechanism to prevent this from being done. It doesn't seem like doing this would be a source of error, which would be a good reason to develop such a mechanism. By contrast, the @FunctionalInterface annotation is intended to guide programmers in the right direction instead of prohibiting something that is arguably wrong but doesn't seem truly harmful.

这篇关于应该 'Comparable&lt;T&gt;'是一个“功能接口"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:27