所谓的函数式接口,固然首先是一个接口,而后就是在这个接口里面只能有一个抽象方法。java
这种类型的接口也称为SAM接口,即Single Abstract Method interfaces。ide
它主要用在Lambda表达式和方法引用(实际上也可认为是Lambda表达式)上。函数
如定义了一个函数式接口以下:测试
@FunctionalInterface interface GreetingService { void sayMessage(String message); }
使用Lambda表达式来表示该接口的一个实现(注:JAVA 8 以前通常是用匿名类实现的):spa
GreetingService greetService1 = message -> System.out.println("Hello " + message);
Java 8为函数式接口引入了一个新注解@FunctionalInterface,主要用于编译级错误检查,加上该注解,当你写的接口不符合函数式接口定义的时候,编译器会报错。code
正确例子,没有报错:继承
@FunctionalInterface interface GreetingService { void sayMessage(String message); }
错误例子,接口中包含了两个抽象方法,违反了函数式接口的定义,Eclipse报错提示其不是函数式接口。接口
提醒:加不加@FunctionalInterface对于接口是否是函数式接口没有影响,该注解知识提醒编译器去检查该接口是否仅包含一个抽象方法ip
函数式接口里是能够包含默认方法,由于默认方法不是抽象方法,其有一个默认实现,因此是符合函数式接口的定义的;ci
以下代码不会报错:
@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等
没有任何参数的函数式接口
Display.java
package us.suselinks.learningjava8; @FunctionalInterface public interface Display { void show(); }
包含一个参数的函数式接口
Circle.java
package us.suselinks.learningjava8; @FunctionalInterface public interface Circle { double area(double radius); }
包含两个参数的函数式接口
Adder.java
package us.suselinks.learningjava8; @FunctionalInterface public interface Adder { int add(int number1, int number2); }
测试函数式接口
FunctionalInterfaceDemo.java
package FunctionalInterfaceDemo; import us.suselinks.learningjava8.Adder; import us.suselinks.learningjava8.Circle; import us.suselinks.learningjava8.Display; public class FunctionalInterfaceDemo { public static void main(String[] args) { // 没有任何参数的函数式接口 Display display = () -> System.out.println("showed up"); display.show(); // 包含一个参数的函数式接口 Circle circle = (num) -> Math.PI * num * num; double res = circle.area(5.0); System.out.println(res); // 包含两个参数的函数式接口 Adder adder = (int a, int b) -> a + b; int rs = adder.add(15, 20); System.out.println(rs); } } 输出: showed up 78.53981633974483 35