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 2026新年祝福语简短语
18 宝宝新年祝福语大全简短