在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 我要结婚祝福语简短
18 婚前女儿生日祝福语简短