Java之函数式接口

函数式接口

概念:有且仅有一个抽象方法的接口;Java中的函数式编程体现就是Lambda,所以函数式接口就是可 以适用于Lambda使用的接口。
常见的函数接口:Runnable,Callable,Compapator,FileFilter
格式:确保接口有且仅有一个抽象方法:

修饰符 interface 接口名称{
	public abstract 返回值类型 方法名称(可选参数信息)
}

@FunctionalInterface注解:

JDK1.8新特性。
作用:用来修饰接口,告诉编译器该接口是函数式接口,如果不是则会编译失败。

自定义函数式接口(无参无返回)

@FunctionalInterface
interface  Eatable{
    void eat();
}

public class DemoStu {
    public static void main(String[] args) {
        test(()-> System.out.println("此肉"));
    }

    public static void test(Eatable e){
        e.eat();
    }
}

自定义函数式接口(有参有返回)

@FunctionalInterface
interface Sumable{
    int sum(int a,int b);
}

public class FunctionInterfaceDemo02 {
    public static void main(String[] args) {
        add(12,23,(a,b)->a+b);
    }
    public static void add(int a,int b,Sumable s){
        System.out.println(s.sum(a,b));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值