Java 8 Lambda表达式-方法重载疑问 [英] Java 8 Lambda expression - Method overloading doubts

查看:192
本文介绍了Java 8 Lambda表达式-方法重载疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Lambda表达式,

I'm trying to learn Lambda expressions,

interface MathOperartor对于int和float类型已重载了operation(),我确定使用Lambda表达式应该可以做到这一点,但似乎不太可能弄清楚这里的问题所在:

interface MathOperartor has operate() overloaded for types int and float, I'm sure this should be possible to do using Lambda expressions, but can't quite seem to figure out what the issue is here:

public static void main(String[] args) {
    LambdaLearning lb = new LambdaLearning();

    MathOperartor add = (a , b )->  a + b;  // error: The target type of this expression must be a functional interface
    MathOperartor sub = (a , b) -> a - b;   // same error
    MathOperartor mul = (a , b) -> a * b;   //  ''
    MathOperartor div = (a , b) -> a / b;   //  ''

    System.out.println(lb.operate(10, 15, add));
    System.out.println(lb.operate(10.5f, 15.5f, sub));
    System.out.println(lb.operate(10, 15, mul));
    System.out.println(lb.operate(10, 15, div));

}

interface MathOperartor{
    public Object operate(int a, int b);
    public Object operate(float a, float b);
}

private Object operate(int a, int b, MathOperartor math){
    return math.operate(a,b);
}
private Object operate(float a, float b, MathOperartor math){
    return math.operate(a,b);
}

请让我知道我在做什么错,并提出解决方法...

Please let me know what I'm doing wrong here and suggest a fix...

更新:

好,所以我理解了功能接口的概念.我的问题还在于实现上述代码中我想做的事情,并且找到了几种实现方法.

Ok, so I understood the concept of Functional Interface, My question was also about achieving what I was trying to do in the above code and I found couple of ways to do it.

感谢大家提供宝贵的答案!

Thank you every one for your valuable answers!

推荐答案

A 功能接口必须是 SAM 接口:

功能接口只有一种抽象方法.由于默认方法具有实现,因此它们不是抽象的.如果接口声明了一个覆盖java.lang.Object的公共方法之一的抽象方法,则该方法也不计入接口的抽象方法计数,因为该接口的任何实现都将具有java.lang.Object或其他地方的实现.

a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

您的接口已声明2个抽象方法,这些方法lambda表达式不知道要去哪里,并且lambda表达式是该接口的一个实例,这意味着必须实现在中声明的所有抽象方法接口.但是您可以添加默认方法来解决这种情况下的问题,例如:

your interface has declared 2 abstract methods that the lambda expression don't know where to going, and the lambda expression is an instance of that interface which means that must implements all the abstract methods declared in the interface. but you can adding default method to solve your problem in this case, for example:

interface MathOperartor{
  //it also can be removed, since a int can cast to a float automatically
  default Object operate(int a, int b){
     return operate((float)a, (float)b);
  }
  public Object operate(float a, float b);
}

这篇关于Java 8 Lambda表达式-方法重载疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆