函数式接口

函数式接口是java8的新特性;

定义:

函数式接口是@FunctionalInterface修饰且必须有一个抽象方法的接口。可以有多个static和default修饰的方法,但是抽象方法只能唯一;

例子:

@FunctionalInterface
public interface FunctionInterfaceTest {

    void test1(String s);


    static String test2(String a,String b){
        return a.concat(b);
    }

    default String test3(String a,String b){
        return b;
    }

    static String test4(String a,String b){
        return a;
    }
    default String test5(String a,String b){
        return b;
    }
}

使用:

public class FunctionalMainTest {

    public static void main(String[] args){
        FunctionInterfaceTest functionInterfaceTest =  s -> System.out.println(s);

        FunctionInterfaceTest functionInterfaceTest1 = s -> System.out.println(s.concat("1"));

        System.out.println(FunctionInterfaceTest.test2("1","2"));
        System.out.println(FunctionInterfaceTest.test4("2","3"));

        functionInterfaceTest1.test1("1");
        System.out.println(functionInterfaceTest1.test3("123","3"));
    }
}

注意了:1、static修饰的方法可以直接使用

              2、static和default修饰的方法必须有实现;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值