简体   繁体   English

功能接口,功能方法和lamda?

[英]Functional Interface , Functional Method and lamda?

I implemented the functional interface, using the functional method in a lambda function.我实现了功能接口,使用 lambda function 中的功能方法。 Everything works fine except i get the error that this inherited abstract method (in my case the functional method) must be implemented.一切正常,除了我得到这个继承的抽象方法(在我的例子中是函数方法)必须实现的错误。 And here i'm confused.在这里我很困惑。 I used the lambda function to implement this functional method and why should i again implement the method.我使用了 lambda function 来实现这个功能方法,为什么我要再次实现这个方法。 How is the implementation of functional methods done correctly?函数式方法的实现如何正确完成? My code:我的代码:

interface MyFunction {
  int getValue(int num); }

class Factorial implements MyFunction {
  public static void main (String [] args) {
        MyFunction myFactorialFunc = (num) -> {
            int fact = 1;
            for(int i = 1; i <= num; i++){
                fact = i * fact;
            }
            return fact;
        };
        System.out.println(myFactorialFunc.getValue(7));
}
}

Thanks for your help.谢谢你的帮助。 :) :)

I guess you must have heard from somewhere that "you can implement single-method interfaces with a lambda expression," and proceeded to try it out by writing this code.我想您一定从某个地方听说过“您可以使用 lambda 表达式实现单方法接口”,然后通过编写此代码进行尝试。 all the while misunderstanding what is meant by that.一直误解那是什么意思。

Lambda expressions doesn't actually allow you to write class MyClass implements SomeInterface without declaring the required method. Lambda 表达式实际上不允许您编写class MyClass implements SomeInterface而不声明所需的方法。 As far as the compiler is concerned, Factorial only has a main method declared, and no getValue method, so it doesn't implement the MyFunction interface.就编译器而言, Factorial只声明了一个main方法,没有getValue方法,所以它没有实现MyFunction接口。

Your code does demonstrate "you can implement single-method interfaces with a lambda expression" though.您的代码确实演示了“您可以使用 lambda 表达式实现单方法接口”。 Here in the main method, you assigned a lambda expression to a variable of type MyFunction :main方法中,您将 lambda 表达式分配给MyFunction类型的变量:

MyFunction myFactorialFunc = (num) -> {
    int fact = 1;
    for(int i = 1; i <= num; i++){
        fact = i * fact;
    }
    return fact;
};

Normally, you have to put an implementation of MyFunction on the right-hand side of = , don't you?通常,您必须将MyFunction的实现放在=的右侧,不是吗? But now, you can just use a lambda expression, This is because the signature of the lambda expression matches the signature of the single method in the interface, so the compiler goes "yeah, that's fine. I'll just use this as the implementation of that method".但是现在,你可以只使用 lambda 表达式,这是因为 lambda 表达式的签名与接口中单个方法的签名匹配,所以编译器会“是的,没关系。我将其用作实现那个方法”。

You defined your Factorial class to implement MyFunction , but it doesn't implement the method of that interface ( int getValue(int num); ).您定义了Factorial class 来实现MyFunction ,但它没有实现该接口的方法( int getValue(int num); )。

The MyFunction instance implemented within your main using a lambda expression doesn't implement that interface for your Factorial class.使用 lambda 表达式在您的main中实现的MyFunction实例不会为您的Factorial class 实现该接口。

Just change it to:只需将其更改为:

class Factorial {
    public static void main (String [] args) {
        MyFunction myFactorialFunc = (num) -> {
            int fact = 1;
            for(int i = 1; i <= num; i++){
                fact = i * fact;
            }
            return fact;
        };
        System.out.println(myFactorialFunc.getValue(7));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM