JAVA 8 函数式接口 - Functional Interface

什么是函数式接口(Functional Interface)

其实之前在讲Lambda表达式的时候提到过,所谓的函数式接口,当然首先是一个接口,然后就是在这个接口里面只能有一个抽象方法

这种类型的接口也称为SAM接口,即Single Abstract Method interfaces。

函数式接口用途

它们主要用在Lambda表达式和方法引用(实际上也可认为是Lambda表达式)上。

如定义了一个函数式接口如下:

    @FunctionalInterface
interface GreetingService
{
void sayMessage(String message);
}

那么就可以使用Lambda表达式来表示该接口的一个实现(注:JAVA 8 之前一般是用匿名类实现的):

GreetingService greetService1 = message -> System.out.println("Hello " + message);

关于@FunctionalInterface注解

Java 8为函数式接口引入了一个新注解@FunctionalInterface,主要用于编译级错误检查,加上该注解,当你写的接口不符合函数式接口定义的时候,编译器会报错。

正确例子,没有报错

    @FunctionalInterface
interface GreetingService
{
void sayMessage(String message);
}

错误例子,接口中包含了两个抽象方法,违反了函数式接口的定义,Eclipse报错提示其不是函数式接口。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtYAAACJCAIAAACHNeiaAAAV6UlEQVR4nO2dXW8c132HJx8g1/kgUgh4L1JYRQoVRpwGNYOaqWqoYkdGm6JsxQiRLWRp+SIikoBLoMpFFKAsrSpNKUToBbfYXhRgC9iWqphm3NJyRnK8shgSkJ24SqpEsszJxbyd95l9mZ3h7vPgB2l3Z+a8zZk5P/7PmV0vBAAAABg5XtUFAAAAgEkECwIAAAAVgAUBAACACsCCAAAAQAVgQQAAAKACsCAAAABQAVgQAAAAqAAsCAAAAFQAFgQAAAAqAAsCAAAAFYAFAQAAgArAggAAAEAFYEEAAACgArAgAAAAUAFYEAAAAKgALAgAAABUABYEAAAAKqB0C7J5cTbh4mbZmfXH5sXZ2dmF9d0R5FLvhogRSlp+swAAwKRSqgXZXV8QxtvNi9kbcZQbwVAnZm3eOrKx1lAUrTEKF8Zdr76QzxkAAEBZlGhB9PFx82IyuJYwdvZWlMqwF2V3faFXI1RCvWrUVAAAMNaUZ0EyvyF9Fg1vW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kWBCGEUH00ThbkYMu55mP9mLqyJJFrmiZafYIFQQghVEdhQeqhzaXD+bMt2g7uo674PJSLEEKotsKCVKx07iZ3Pan+sMzl47YHYeKnb5Q0sSAIIYTqIyzIBAkLghBCqD7yQgAAAICRgwUBAACACsCCAAAAQAVgQQAAAKACSrUgbd/zPM9rtIIycwEAAICDR/lRkKDV8Dy/XXo+AAAAcIAYwURM0GoQCAEAAAAJLAgAAABUABYEAAAAKmAUT8S0fZakAgAAgMSoHsoNWg2PVakAAAAQMwoLwkwMAAAAKLAWBAAAACoACwIAAAAVgAUBAACACsCCAAAAQAXwBe0AAABQAfxMHQAAAFTAqL4XBAAAAEAACwIAAAAVgAUBAACACsCCAAAAQAVgQQAAAKACsCAAAABQAVgQAAAAqAAsCAAAAFQAFgQAAAAqAAsCAAAAFfA7d/Cp/LOP7UQAAAAASUVORK5CYII=" alt="" />

提醒:加不加@FunctionalInterface对于接口是不是函数式接口没有影响,该注解知识提醒编译器去检查该接口是否仅包含一个抽象方法

函数式接口里允许定义默认方法

函数式接口里是可以包含默认方法,因为默认方法不是抽象方法,其有一个默认实现,所以是符合函数式接口的定义的;

如下代码不会报错:

    @FunctionalInterface
interface GreetingService
{
void sayMessage(String message); default void doSomeMoreWork1()
{
// Method body
} default void doSomeMoreWork2()
{
// Method body
}
}

函数式接口里允许定义静态方法

函数式接口里是可以包含静态方法,因为静态方法不能是抽象方法,是一个已经实现了的方法,所以是符合函数式接口的定义的;

如下代码不会报错:

    @FunctionalInterface
interface GreetingService
{
void sayMessage(String message);
static void printHello(){
System.out.println("Hello");
}
}

函数式接口里允许定义java.lang.Object里的public方法

函数式接口里是可以包含Object里的public方法,这些方法对于函数式接口来说,不被当成是抽象方法(虽然它们是抽象方法);因为任何一个函数式接口的实现,默认都继承了Object类,包含了来自java.lang.Object里对这些抽象方法的实现;

如下代码不会报错:

    @FunctionalInterface
interface GreetingService
{
void sayMessage(String message); @Override
boolean equals(Object obj);
}

JDK中的函数式接口举例

java.lang.Runnable,

java.awt.event.ActionListener,

java.util.Comparator,

java.util.concurrent.Callable

java.util.function包下的接口,如Consumer、Predicate、Supplier等

参考资料

https://sanaulla.info/2013/03/21/introduction-to-functional-interfaces-a-concept-recreated-in-java-8/

http://howtodoinjava.com/java-8/functional-interface-tutorial/

上一篇:iOS UITableView删除cell分割线


下一篇:iOS UITableView划动删除的实现