为什么 Java 8 中的函数式接口只有一个抽象方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23342499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:05:49  来源:igfitidea点击:

Why Functional Interfaces in Java 8 have one Abstract Method?

javajava-8functional-interface

提问by Harmeet Singh Taara

As we know in Java 8, the concept of functional interfaces are introduced. A Functional Interface has one abstractmethod and several default or static methods are possible.

正如我们在 Java 8 中所知道的,引入了函数式接口的概念。一个函数式接口有一个abstract方法,并且有几个默认或静态方法是可能的。

But why should a Functional interface have only one abstract method? If Interface has more then one abstract method, why is this not a Functional Interface?

但是为什么一个 Functional 接口应该只有一个抽象方法呢?如果接口有多个抽象方法,为什么这不是功能接口?

回答by AdityaKeyal

The functional interface also known as Single Abstract Method Interfacewas introduced to facilitate Lambda functions. Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one abstract method. For more details refer here.

引入了函数接口,也称为单一抽象方法接口,以促进 Lambda 函数。由于 lambda 函数只能提供 1 个方法的实现,因此函数式接口必须只有一个抽象方法。有关更多详细信息,请参阅此处

Edit-> Also worth noting here is that, a functional interface can have a default implementation in the interface. You will find a lot more details on the implementation on the link above.

编辑-> 这里还值得注意的是,功能接口可以在接口中具有默认实现。您将在上面的链接中找到有关实施的更多详细信息。

回答by Sayat Satybald

Functional Interface lets us call an object as if it were a function, which lets us pass verbs(functions) around our program rather than nouns(objects). Implementations of Functional Interface perform a single well-defined action, as any method should, with a name like run, execute, perform, apply, or some other generic verb.[1]

函数式接口让我们可以像调用函数一样调用对象,这让我们可以在我们的程序中传递动词(函数)而不是名词(对象)。函数式接口的实现执行单个明确定义的操作,就像任何方法应该的那样,使用名称如运行、执行、执行、应用或其他一些通用动词。 [1]

  1. Functional Programming Patterns in Scala and Clojure.
  1. Scala 和 Clojure 中的函数式编程模式。

回答by sairam

Writing Lamba expression meaning we are implementing the interface that is functional interface. It should have one abstract method because at the time of lambda expression, we can provide only one implementation at once. So in the code snippet posted in the below question, at any time we are giving only one implementation while declaring Lambda where we will have to implement for two abstract methods.

编写 Lamba 表达式意味着我们正在实现的接口是函数式接口。它应该有一个抽象方法,因为在 lambda 表达式的时候,我们一次只能提供一个实现。因此,在以下问题中发布的代码片段中,我们在任何时候都只给出一个实现,同时声明 Lambda,我们必须在其中实现两个抽象方法。

Why not multiple abstract methods in Functional Interface in Java8?

为什么在 Java8 的函数式接口中没有多个抽象方法?

回答by sn.anurag

If Java would have allowed having two abstract methods, then lambda expression would be required to provide an implementation of both the methods. Because, calling method won't know, which method to call out of those 2 abstract methods. It could have called the one which is not implemented. For example

如果 Java 允许有两个抽象方法,那么将需要 lambda 表达式来提供这两个方法的实现。因为,调用方法不知道从这两个抽象方法中调用哪个方法。它可以称为未实施的那个。例如

If Java would have allowed this kind of functional interface

如果 Java 允许这种功能接口

@FunctionalInterface
interface MyInterface {
    void display();
    void display(int x, int y);
}

Then on implementing the following would have not been possible.

那么在实施以下内容时是不可能的。

public class LambdaExpression {
    public static void main(String[] args) {
        MyInterface ref = () -> System.out.print("It is display from sout");
        ref.display(2, 3);

    }
}

Since display(int x, int y) is not implemented, it will give the error. That is why the functional interface is a single abstract method interface.

由于 display(int x, int y) 没有实现,它会给出错误。这就是为什么功能接口是单个抽象方法接口的原因。