为什么 Java 8 中的函数式接口有一个抽象方法? [英] Why Functional Interfaces in Java 8 have one Abstract Method?

查看:33
本文介绍了为什么 Java 8 中的函数式接口有一个抽象方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

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

The functional interface also known as Single Abstract Method Interface was 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.

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

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.

这篇关于为什么 Java 8 中的函数式接口有一个抽象方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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