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 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短