仅用一种抽象方法定义的接口称为功能接口。使用 @FunctionalInterface批注标记功能接口不是强制性的,编译器不会抛出任何错误。但是,最好使用@FunctionalInterface批注以避免意外添加其他方法。如果一个接口使用@FunctionalInterface注释和一个以上的抽象方法进行注释,则它将引发编译时错误。
@FunctionalInterface
interface interface_name {
//仅一个abstarct方法声明
}@FunctionalInterfaceinterface Shape {
void printArea(int x);
}
public class SquareTest {
public static void main (String args[]) {
Shape square = (x) -> { // Lambda Expression
System.out.println("The area of a square is: "+ x*x);
};
square.printArea(7);
}
}输出结果
The area of a square is: 49