[英]When we add two abstract methods in an interface and implement just one method then why can't we implement the other method using lambda?
I am practising lambda functions, what I know is to work with them we need to create a functional interface, that contains only one unimplemented abstract method.我正在练习 lambda 函数,我知道要使用它们,我们需要创建一个函数接口,它只包含一个未实现的抽象方法。
So I was wondering why does java don't allow more than one abstract method even if they have different signatures.所以我想知道为什么 java 不允许多个抽象方法,即使它们具有不同的签名。 So I read through related questions on Stack Overflow.
所以我通读了有关 Stack Overflow 的相关问题。 And this is what I got.
这就是我得到的。 similar question
类似的问题
The answer would be that in order to create a valid implementation, you would need to be able to pass N lambdas at once and this would introduce a lot of ambiguity and a huge decrease in readability.
答案是,为了创建一个有效的实现,您需要能够一次传递 N 个 lambda,这会引入很多歧义并大大降低可读性。
Now I did a little experiment, I created an interface with two implemented abstract methods with different signatures.现在我做了一个小实验,我创建了一个接口,其中包含两个具有不同签名的已实现抽象方法。
This is myInterface
interface这是
myInterface
接口
interface MyInterface {
abstract void func();
abstract void func(int i);
}
And implemented one function as we normally do and the other as a lambda expression并像我们通常做的那样实现了一个 function 和另一个 lambda 表达式
This is Lambda
class这是
Lambda
class
class Lambda implements MyInterface{
public void doWork(MyInterface myInterface) {
myInterface.func();
}
public static void main(String[] args) {
Lambda lambda = new Lambda();
lambda.doWork( e -> System.out.println("func:" + e ));
}
@Override
public void func() {
System.out.println("func");
}
}
Obviously this code doesn't work.显然这段代码不起作用。 I just can't understand even though I have two different signatures and even if one of them is implemented normally then why can't we implemented the other remaining method with a lambda.
我只是无法理解,即使我有两个不同的签名,即使其中一个被正常实现,那么为什么我们不能用 lambda 实现另一个剩余的方法。 Is it something to do with code quality and maintainability?
它与代码质量和可维护性有关吗? Can anyone explain I can't get my head around it?
谁能解释我无法理解它? Pls, give an example if possible.
如果可能的话,请举个例子。
You can create a sub-interface of MyInterface
with all but one methods implemented using the default
keyword.您可以创建
MyInterface
的子接口,其中所有方法都使用default
关键字实现,但只有一种方法。 It allows you to provide default implementations that interface implementers "inherit" if they do not provide own implementations.如果他们不提供自己的实现,它允许您提供接口实现者“继承”的默认实现。 Thus methods with default implementation are not abstract anymore.
因此,具有默认实现的方法不再是抽象的。 If you default-implement all but one interface in s sub-interface effectively you end up with a functional interface (a single abstract method interface).
如果您在 s 子接口中有效地默认实现除一个接口之外的所有接口,您最终会得到一个功能接口(单个抽象方法接口)。 And this allows you to use lambda expressions.
这允许您使用 lambda 表达式。
interface PartialInterface extends MyInterface {
@Override
default void func() {
System.out.println("func");
}
}
class Lambda {
public void doWork(PartialInterface myInterface) {
// both methods from the interface can be used
myInterface.func(); // the default implementation "inherited" from PartialInterface
myInterface.func(42); // the single abstract method (inherited by PartialInterface from myInterface) overridden by the lambda expression
}
public static void main(String[] args) {
Lambda lambda = new Lambda();
lambda.doWork(e -> System.out.println("func:" + e));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.