Lambda表达式在一行中使用接口。

huangapple go评论53阅读模式
英文:

Lambda expression using interface in one line

问题

I'm learning about interfaces and I faced a problem which I cannot understand. It's about lambda expression which I learn that can be used in one line, but I've create a case, where this expression does not working and I don't know where I've made a mistake.

我正在学习接口,但我遇到了一个问题,我无法理解。这个问题与我学习的可以在一行中使用的 lambda 表达式有关,但我创建了一个情况,其中这个表达式不起作用,我不知道我在哪里出错了。

I have defined this interface:

我定义了这个接口:

    int add(int a, int b);
}

Next I have defined class in which I want to test this interface with adder method.

接下来,我定义了一个类,我想在其中使用 adder 方法来测试这个接口。

    public static void main(String[] args) {
        int x;
        x = adder((a, b) -> return a+b);
    }

    public static int adder(MathOperations mo){
        return mo.add(3, 5);
    }
}

Unfortunately the line with the x assignment doesn't work and I cannot figure out where I've made a mistake. The compiler does not recognize the a and b variable in the return statement. I know that I can make this assignment with brackets but I'm curious if I can do this in one line.

不幸的是,x 赋值的那一行不起作用,我无法弄清楚我在哪里出错了。编译器无法识别 return 语句中的 ab 变量。我知道我可以使用大括号来进行赋值,但我很好奇是否可以在一行中完成这个操作。

英文:

I'm learning about interfaces and I faced a problem which I cannot understand. It's about lambda expression which I learn that can be used in one line, but I've create a case, where this expression does not working and I don't know where I've made a mistake.

I have defined this interface:

public interface MathOperations {
    int add(int a, int b);
}

Next I have defined class in which I want to test this interface with adder method.

public class App {
    public static void main(String[] args) {
        int x;
        x = adder((a, b) -> return a+b);
    }

    public static int adder(MathOperations mo){
        return mo.add(3, 5);
    }
}

Unfortunately the line with the x assignment doesn't work and I cannot figure out where I've made a mistake. The compiler does not recognize the a and b variable in the return statement. I know that I can make this assignment with brackets but I'm curious if I can do this in one line.

答案1

得分: 2

@FunctionalInterface
interface MathOperations {
    int add(int a, int b);
}

这是两种实现你想要的方法
- 要创建一个lambda你需要指定接口类型并提供对`a``b`的操作定义
- 然后你可以调用lambda并获得值
```java
MathOperations compute = (a, b) -> a + b;
x = compute.add(2, 3);
System.out.println(x);

上述代码实际上在幕后执行以下操作。

  • 这里使用接口定义了一个匿名类,并使用new关键字实例化了它(该类包含了实现的方法)。
  • 在Java 8引入FuntionalInterfaces和lambda的概念之前,实现接口是标准的做法。
  • 然后调用adder方法,提供刚刚创建的mo实例,或者使用之前创建的compute lambda调用方法。
MathOperations mo = new MathOperations() {
    public int add(int a, int b) {
        return a + b;
    }
};

System.out.println(adder(mo));
System.out.println(adder(compute));

上述三个打印语句的输出如下:

5
8
8
public static int adder(MathOperations mo) {
    return mo.add(3, 5);
}

注意: 依我看来,可以根据已经定义的API函数接口的示例创建更通用的接口。MathOperation的名称是可以接受的,但add方法太具体了(函数接口只能包含一个抽象方法)。所以,不要有一个add方法,而是有一个compute或类似的方法,让lambda的名称决定它的操作

MathOperation add = (a, b) -> a + b;
int sum = add.compute(10, 20); // 30
MathOperation sub = (a, b) -> a - b;
int diff = sub.compute(10, 20); // -10
英文:
@FunctionalInterface
interface MathOperations {
    int add(int a, int b);
}

Here are two ways to do what you want.

  • To create a lambda, you need to specify the interface type and provide the definition of what to do with a and b.
  • then you can invoke the lambda and get the value.
MathOperations compute = (a,b)->a+b;
x = compute.add(2,3);
System.out.println(x);

The above does essentially the following behind the scenes.

  • Here an anonymous class is defined using the interface and instantiated using the new keyword. (the class contains the implemented method).
  • Implementing the interface was standard until the concept of FuntionalInterfaces and lambdas were introduced in Java 8.
  • Then call the adder method supplying the instance mo just created or call the method with the previously created compute lambda.
MathOperations mo =new MathOperations() {
                          public int add(int a,int b) {
                              return a+b;
                          }};

 System.out.println(adder(mo));
 System.out.println(adder(compute));      
    

output of above three print statements

5
8
8
public static int adder(MathOperations mo){
    return mo.add(3, 5);
}

Note: Imo, a more versatile interface can be created by following examples from the API functional interfaces already defined. The name MathOperation is fine but the add method is too specific (and functional interfaces may only contain one abstract method). So instead of having an add method, have a compute or similar method and let the name of the lambda dictate its operation.

MathOperation add = (a,b)->a+b;
int sum = add.compute(10,20); // 30
MathOperation sub = (a,b)->a-b;
int diff = sub.compute(10,20) // -10

huangapple
  • 本文由 发表于 2023年2月6日 02:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定