简体   繁体   English

当我们在一个接口中添加两个抽象方法并只实现一个方法时,为什么我们不能使用 lambda 实现另一个方法?

[英]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.

相关问题 为什么我们不能在不修改“ public”的情况下实现从接口到抽象类的方法? - why we can't implement methods from interface to abstract class, without modifying “public”? 为什么我们不能在功能界面中重载抽象方法? (JAVA) - Why can't we overload a abstract method in a functional interface? (Java) 当我们在类中定义接口的抽象方法时,我们是“覆盖”或“实现”还是简单地说“定义”那些方法? - When we define the abstract methods of an interface in a class do we 'override' or 'implement' or simply say 'define' those methods? 如果我们在接口中添加方法,我们如何在多个类中实现方法 - how can we implement methods in multiples classes if we add methods in interface Java:如果我们必须自己实现抽象方法,如何实现多个接口实现多重继承? - Java: How does implementing more than one interface achieve multiple inheritance if we have to implement the abstract method ourselves? 为什么我们不能在没有匿名类方法的情况下在java中实例化接口或抽象类? - Why can't we instantiate an interface or an abstract class in java without an anonymous class method? 什么时候以及为什么我们必须实现Comparable接口? - When and why we have to implement Comparable interface? 如果我们可以改用空的具体方法,为什么还要强迫自己在具体子类中实现抽象方法呢? - Why force ourselves to implement abstract methods in concrete subclasses, if we can use empty concrete methods instead? 如何在接口中实现抽象方法? - How to implement the abstract method in interface? 我们可以在jsp中实现一个接口吗? - Can we implement an interface in jsp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM