为什么Java8中的Functional Interface中没有多个抽象方法? [英] Why not multiple abstract methods in Functional Interface in Java8?

查看:170
本文介绍了为什么Java8中的Functional Interface中没有多个抽象方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@FunctionalInterface
interface MyLambda {
    void apply1();
    int apply2(int x, int y);
}

现在使用Lambda表达式为什么Java不能允许低于2表示区分两者:

Now using the Lambda expressions why can't Java allow below two as it clearly differentiate between the two:

MyLambda ml1 = () -> System.out.println("Hello");
MyLambda ml2 = (x, y) -> x+y;


推荐答案

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

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 question, at any time we are giving only one implementation while declaring Lambda where we will have to implement for two abstract methods.

感谢您的帮助。

这篇关于为什么Java8中的Functional Interface中没有多个抽象方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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