[英]What's the use case for Functional interface other than different implementations using Lambda expressions?
我想知道除了使用lambda表達式的不同實現之外,功能接口的用例是什么?
這是功能接口的基本示例:
/*Basic example for Functional interface with Lambda expression*/
public class Lambda_test {
/*
* Functional Interface annotation will not allow to declare more than one
* abstract method which is obvious for the concept
*/
@FunctionalInterface
interface NameTest {
// One abstract method
abstract String MyName(String name);
}
public static void main(String[] args) {
NameTest nametest = (name) -> "Ashwin " + name + "!";
System.out.println("My name is " + nametest.MyName("Savaj"));
}
}
根據Java規范https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html
功能接口是僅具有一個抽象方法(除Object的方法之外)的接口,因此表示單個功能協定。
由於默認方法具有實現,因此它們不是抽象的。
如果是@FunctionalInterface:
除非出現以下情況,否則編譯器將產生錯誤消息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.