在Java中使用@FunctionalInterface批注标记功能接口是否是强制性的?
仅用一种抽象方法定义的接口称为功能接口。使用 @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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短