jdk8?FunctionalInterface注解源碼解讀
源碼
package java.lang; import java.lang.annotation.*; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FunctionalInterface {}
Conceptually, a functional interface has exactly one abstract method.
Since default methods have an implementation, they are not abstract.
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
打撈要點(diǎn)之后有三點(diǎn)
- 要聲明FunctionalInterface的接口只能有1個(gè)抽象方法;
a functional interface has exactly one abstract method.
- 因?yàn)閐efault方法有實(shí)現(xiàn),所以它也不是抽象方法, 所以不在列;
Since default methods have an implementation, they are not abstract.
- 因?yàn)檫@里限定抽象方法只能有1個(gè),那么如果我的接口里聲明了一個(gè) 抽象方法
"String toString();
"或者"boolean equals(Object obj);
"這樣的,算不算呢? 答案是:不算這個(gè)
,因?yàn)槿魏螌?shí)現(xiàn)接口的對(duì)象都會(huì)繼承Object,所以這些public的Object的方法,不算在列的。
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
接口聲明
也就是說,你的接口可以聲名如下:
/** * Created by niewj on 2017/10/26. */ @FunctionalInterface public interface FunctionalInterfaceSummary { int doSum(int x, int y); // 抽象方法 String toString(); // 不占1的數(shù)額 boolean equals(Object obj); // 不占1的數(shù)額 // boolean equals(); // 占1的數(shù)額,這不是Object方法 default void display() { System.out.println("show sth.."); } }
小結(jié)
1. 接口只限一個(gè)抽象方法,可注解@FunctionalInterface 限定;
2. 聲明函數(shù)式接口之后,有以下特征:
a. 只有一個(gè)抽象方法;
b. Object類的public的方法也可以在接口里寫出來,因?yàn)檫@不算;
c. default方法不算, 因?yàn)樗鼈兪莍mplementation。不是abstract!
以上就是jdk8 FunctionalInterface注解源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于jdk8 FunctionalInterface注解的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot?Profile多環(huán)境配置方式
這篇文章主要介紹了SpringBoot?Profile多環(huán)境配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Springboot 使用 maven-resources-plugin 打包變量替換ja
這篇文章主要介紹了Springboot 使用 maven-resources-plugin 打包變量替換jar沒有打包進(jìn)去、Jar包沒有被使用的解決方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-08-08解決springboot?druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連問題
這篇文章主要介紹了解決springboot?druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)雙向鏈表功能
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)雙向鏈表功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11