Java 8中的功能接口是什么?
功能接口是只有一种抽象方法的接口。它们势必展现出独特的功能。Runnable,Comparable和Predicate是功能接口的一些示例。
@FunctionalInterfaceAnnotation用于确保功能接口不能具有多个抽象方法。从Java8开始,我们可以将lambda表达式分配给功能接口对象。
让我们看看用户定义的功能接口如何工作-
示例
@FunctionalInterface
interface Cube {
int compute(int x);
}
public class Example {
public static void main(String args[]) {
int p = 6;
Cube c = r -> r * r * r; // lambda expression which defines the compute method
int result = c.compute(p);
System.out.println(result);
}
}输出结果
输出如下-
216
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短