简体   繁体   English

函数式接口中的函数如何执行 lambda 函数?

[英]How does a function from a functional interface perform a lambda function?

I'm trying to understand functional interfaces and lambda functions.And I can't find an explanation of how a function in a functional interface connects it with a lambda function , that is, let's say there is such a functional interface我正在尝试理解函数式接口和 lambda 函数。而且我找不到函数式接口中的函数如何将它与 lambda 函数连接的解释,也就是说,假设有这样一个函数式接口

@FunctionalInterface
interface MyPredicate {
    boolean test(Integer value);
}

and now we assign the variables of the functional interface to the lambda function:现在我们将函数接口的变量分配给 lambda 函数:

public class Tester {
    public static void main(String[] args) throws Exception {
        MyPredicate myPredicate = x -> x > 0;
        System.out.println(myPredicate.test(10));   //true
    }
}

I'm exactly wondering why when calling myPredicate.test(10) a call is being made x > 0 .我很想知道为什么在调用myPredicate.test(10)时会调用x > 0 That is, do I understand correctly that when we assign a lambda function, the compiler somehow connects the function from the functional interface with the body of the lambda function?it's just that inheritance and override are usually used for this ,but here the compiler does it or how?I will be glad to have explanations to understand this issue也就是说,我是否正确理解当我们分配一个 lambda 函数时,编译器以某种方式将函数接口中的函数与 lambda 函数的主体连接起来?只是继承和覆盖通常用于此,但编译器在这里做了它或如何?我会很高兴有解释来理解这个问题

What exactly IS a lambda expression in pre-Java8 terms?在 Java8 之前的术语中,lambda 表达式究竟是什么?

It's an instance of an anonymous class implementing the interface needed in the current context.它是一个匿名类的实例,实现了当前上下文中所需的接口。

So, to understand所以,要明白

MyPredicate myPredicate = x -> x > 0;

we have to consider some different aspects.我们必须考虑一些不同的方面。

Here, the lambda expression x -> x > 0 is required to give a result compatible with MyPredicate .在这里,需要 lambda 表达式x -> x > 0来给出与MyPredicate兼容的结果。 So, by considering the context where the expression is used, it is equivalent to因此,通过考虑使用该表达式的上下文,它等价于

MyPredicate myPredicate = new MyPredicate() {
    boolean test(Integer whateverName) {
        // some method body
    }
};

Now, the lambda expression fills the body of that method.现在,lambda 表达式填充了该方法的主体。 The x -> part defines the parameter name, so now we have x ->部分定义了参数名称,所以现在我们有

MyPredicate myPredicate = new MyPredicate() {
    boolean test(Integer x) {
        // some method body
    }
};

And the x > 0 defines the body and the value to be returned:x > 0定义了主体和要返回的值:

MyPredicate myPredicate = new MyPredicate() {
    boolean test(Integer x) {
        return x > 0;
    }
};

So, when you call myPredicate.test(10) , there's nothing special going on.所以,当你调用myPredicate.test(10) ,没有什么特别的。 The (anonymous-class) instance in myPredicate gets its test() method called with argument 10 . myPredicate的(匿名类)实例使用参数10调用其test()方法。 It's only that this instance has been created using the lambda-expression syntax.只是这个实例是使用 lambda 表达式语法创建的。

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

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