问题描述
@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中没有多个抽象方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!