为什么我需要一个函数式接口来使用 lambdas? [英] Why do I need a functional Interface to work with lambdas?

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

问题描述

我认为这个问题已经在某个地方了,但我找不到.

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

我不明白,为什么有必要使用函数式接口来处理 lambda.考虑以下示例:

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! :)

抱歉,我显然不是母语人士,但准确地说:

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 接口"创建一个方法.

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");

您为 TestInterfacevoid hans() 方法提供了一个实现.

You give an implementation to the void hans() method of the 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天全站免登陆