java8自定义函数接口

java8开始可以自定义函数式接口,方便开发人员使用lambda表达式,简化了代码量。

1.首先定义一个函数式接口(使用泛型能过更好的适配所有对象的操作)

    

/**
 * Represents a function that accepts two argument and produces a result.
 *
 *
 * whose functional method is {@link #Do(Object)}.
 *
 * @param <A> the first of the input to the function
 * @param <B> the second of the result of the function
 * @param <C>  the return value
 * @since 1.8
 */
@FunctionalInterface
public interface MyFunction<A,B,C> {

    C Do(A a,B b);
}

2.直接运行main方法

public class MyFunctionTest {

    public static void main(String[] args){

        MyFunction fun = (a,b) -> (int) a+ (int)b;
        System.out.println(fun.Do(1,2));
    }
}

以上是最简单的写法,显式的调用了Do方法。

剩余的代码封装,其余的接口和抽象类的定义就看个人功底了。想要熟悉函数接口使用,可以去看看集合类的stream使用。

也可以参考http://www.runoob.com/java/java8-functional-interfaces.html

https://github.com/winterbe/java8-tutorial

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值