java8中的lambda定义的函数该如何引用
PHP中文网
PHP中文网 2017-04-18 09:18:55
0
2
298

定义好的函数,不知道该如何使用。

// 不知道怎么引用
BinaryOperator<Long> add = (x, y) -> x + y;
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
伊谢尔伦
public class Main {
    
    private long t, u;
    
    private Long test(BinaryOperator<Long> b) {
        return b.apply(t, u);
    }
    public static void main(String[] args) {
        
        Main m = new Main();
        m.t = 1; m.u = 2;
        BinaryOperator<Long> b = (x, y) -> x + y;
        System.out.println(m.test(b));
    }
}

It is useless to pull it out alone. Lambda only defines the operation method of data, that is, it defines a function. Specifically where to use it, you need to define a method with a lambda expression (functional interface) as the parameter, and then call the actual operation of lambda (which function in the interface definition) inside the method, such as accept.

刘奇

Lambda定义的并不是函数,它只是匿名类的缩写方式,其生成的还是一个对象。就如你的例子中,它生成的一个BinaryOperator<Long>Object, then it is an instance object of this class. How to use it is the same as how to use an object.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!