为什么我需要一个功能界面来处理lambdas? [英] Why do I need a functional Interface to work with lambdas?

查看:111
本文介绍了为什么我需要一个功能界面来处理lambdas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题已经存在,但我无法找到它。

I think this question is already somewhere out there, but I wasn't able to find it.

我不明白,为什么有必要与lambdas一起使用的功能界面。请考虑以下示例:

I don't understand, why it's necessary to have a functional interface to work with lambdas. Consider the following example:

public class Test {

    public static void main(String...args) {
        TestInterface i = () -> System.out.println("Hans");
//      i = (String a) -> System.out.println(a);

        i.hans();
//      i.hans("Hello");
    }
}

public interface TestInterface {
    public void hans();
//  public void hans(String a);
}

这没有问题,但如果你取消评论注释行,它就不会吨。为什么?根据我的理解,编译器应该能够区分这两种方法,因为它们具有不同的输入参数。为什么我需要一个功能界面并炸掉我的代码?

This works without problems, but if you uncomment the commented lines, it doesn't. Why? In my understanding, the compiler should be able to distinguish the two methods, since they have different input-parameters. Why do I need a functional interface and blow up my code?

编辑:链接的副本没有回答我的问题,因为我问的是不同的方法参数。但是我在这里得到了一些非常有用的答案,感谢所有帮助过的人! :)

The linked duplicates didn't answer my question because I'm asking about different method-parameters. But I got some really useful answers here, thanks to everyone who helped! :)

EDIT2:对不起,我显然不是以母语为母语的人,但为了准确自己:

Sorry, I'm obviously not a native speaker, but to precise myself:

public interface TestInterface {
    public void hans();                 //has no input parameters</br>
    public void hans(String a);         //has 1 input parameter, type String</br>
    public void hans(String a, int b);  //has 2 input parameters, 1. type = String, 2. type = int</br>
    public void hans(int a, int b);     //has also 2 input parameters, but not the same and a different order than `hans(String a, int a);`, so you could distinguish both
}

public class Test {

    public static void main(String...args) {
        TestInterface i = () -> System.out.println("Hans");
        i = (String a) -> System.out.println(a);
        i = (String a, int b) -> System.out.println(a + b);
        i = (int a, int b) -> System.out.println(a);

        i.hans(2, 3);   //Which method would be called? Of course the one that would take 2 integer arguments. :)
    }
}

所有我问的是关于论点。方法名称无关紧要,但每个方法都采用不同参数的唯一顺序,因此,Oracle可以实现此功能,而不是仅根据Lambda-Interface使单个方法成为可能。

All I'm asking is about the arguments. The method name doesn't matter, but each method takes an unique order of different arguments and because of that, Oracle could have implemented this feature instead just making a single method possible per "Lambda-Interface".

推荐答案

当你写:

TestInterface i = () -> System.out.println("Hans");

您为 void hans() TestInterface 的方法。

如果可以将lambda表达式赋给具有多个摘要的接口方法(即非功能接口),lambda表达式只能实现其中一个方法,其他方法未实现。

If you could assign a lambda expression to an interface having more than one abstract method (i.e. a non functional interface), the lambda expression could only implement one of the methods, leaving the other methods unimplemented.

你不能通过分配两个方法来解决它lambda表达式对同一个变量有不同的签名(就像你不能将两个对象的引用分配给一个变量,并期望该变量一次引用这两个对象)。

You can't solve it by assigning two lambda expressions having different signatures to the same variable (Just like you can't assign references of two objects to a single variable and expect that variable to refer to both objects at once).

这篇关于为什么我需要一个功能界面来处理lambdas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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