Java基础篇之java8新特性:自定义函数式接口

2020-01-13  本文已影响0人  writeanewworld

1.前言

1)、自定义一个函数式接口,加深对lambda的理解。

2.例子

//定义函数式接口
@FunctionalInterface
public interface Java8Fun<R,T> {

    //运算
    R operator(T t1,T t2);

}

//定义方法 使用到该函数式接口
public class opeator {

    public static Integer operator(Integer a,Integer b,Java8Fun<Integer,Integer> of){
        return  of.operator(a,b);
    }

}
//使用
public class test {
    public static void main(String[] args) {
        //lambda中传递行为
        Integer res = operator(1,2,(Integer a,Integer b)->a+b);
        System.out.println(res);
    }

}

上一篇 下一篇

猜你喜欢

热点阅读