其实以前在讲Lambda表达式的时候提到过,所谓的函数式接口,固然首先是一个接口,而后就是在这个接口里面只能有一个抽象方法。java
这种类型的接口也称为SAM接口,即Single Abstract Method interfaces。ide
它们主要用在Lambda表达式和方法引用(实际上也可认为是Lambda表达式)上。函数
如定义了一个函数式接口以下:spa
@FunctionalInterface interface GreetingService { void sayMessage(String message); }
那么就能够使用Lambda表达式来表示该接口的一个实现(注:JAVA 8 以前通常是用匿名类实现的):code
GreetingService greetService1 = message -> System.out.println("Hello " + message);
Java 8为函数式接口引入了一个新注解@FunctionalInterface,主要用于编译级错误检查,加上该注解,当你写的接口不符合函数式接口定义的时候,编译器会报错。blog
正确例子,没有报错:继承
@FunctionalInterface interface GreetingService { void sayMessage(String message); }
错误例子,接口中包含了两个抽象方法,违反了函数式接口的定义,Eclipse报错提示其不是函数式接口。接口
提醒:加不加@FunctionalInterface对于接口是否是函数式接口没有影响,该注解知识提醒编译器去检查该接口是否仅包含一个抽象方法ip
函数式接口里是能够包含默认方法,由于默认方法不是抽象方法,其有一个默认实现,因此是符合函数式接口的定义的;编译器
以下代码不会报错:
@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"); } }
函数式接口里是能够包含Object里的public方法,这些方法对于函数式接口来讲,不被当成是抽象方法(虽然它们是抽象方法);由于任何一个函数式接口的实现,默认都继承了Object类,包含了来自java.lang.Object里对这些抽象方法的实现;
以下代码不会报错:
@FunctionalInterface interface GreetingService { void sayMessage(String message); @Override boolean equals(Object obj); }
java.lang.Runnable,
java.awt.event.ActionListener,
java.util.Comparator,
java.util.concurrent.Callable
java.util.function包下的接口,如Consumer、Predicate、Supplier等