函数式接口@FunctionalInterface使用示例
通過(guò)JDK8源碼javadoc,可以知道@FunctionalInterface有以下特點(diǎn):
該注解只能標(biāo)記在"有且僅有一個(gè)抽象方法"的接口上,表示函數(shù)式接口。
JDK8接口中的靜態(tài)方法和默認(rèn)方法,都不算是抽象方法。
接口默認(rèn)繼承java.lang.Object,所以如果接口顯示聲明覆蓋了Object中的方法,那么也不算抽象方法。
該注解不是必須的,如果一個(gè)接口符合"函數(shù)式編程"定義,那么加不加該注解都沒(méi)有影響。加上該注解能夠更好地讓編譯器進(jìn)行檢查,如果編寫(xiě)的不是函數(shù)式接口,但是加上了@FunctionalInterface 那么編譯器會(huì)報(bào)錯(cuò)。
代碼說(shuō)明:使用Lambda表達(dá)式。一般的格式是 ()->{} ,如果{}里面只有一行代碼,則{}可以省略。 (->左邊的()表示方法體,如果有形參,則在()中添加形參,->右邊{}表示具體邏輯。如果方法體返回值是void,則甚至可以在{}中不寫(xiě)任何邏輯(當(dāng)然也要結(jié)合場(chǎng)景)。返回值如果有值,則需要寫(xiě)具體的邏輯,return處理后的值。)理解這里非常重要!!!!
演示一、請(qǐng)求參數(shù)、返回參數(shù)均有值的接口
package com.calvin.currency.function;
/**
?* @Title CustomFuctionInterface
?* @Description 自定義函數(shù)式接口之演示一、請(qǐng)求參數(shù)、返回參數(shù)均有值的接口
?* @author calvin
?* @date: 2020/3/3 1:02 AM?
?*/
@FunctionalInterface
public interface CustomFuctionInterface {
? ? String printStr(String str1, String str2);
}
代碼測(cè)試
@Test
public void test1() {
? ? CustomFuctionInterface customFuctionInterface = (str1, str2) -> "hello " + str1 + str2;
? ? String printStr = customFuctionInterface.printStr("A&", "B");
? ? System.out.println("printStr = " + printStr);
}
控制臺(tái)輸出結(jié)果:
演示二、請(qǐng)求參數(shù)沒(méi)有值、返回參數(shù)有值的接口
package com.calvin.currency.function;
/**
?* @Title CustomFuctionInterface
?* @Description 自定義函數(shù)式接口之演示二、請(qǐng)求參數(shù)沒(méi)有值、返回參數(shù)有值的接口
?* @author calvin
?* @date: 2020/3/3 1:06 AM
?*/
@FunctionalInterface
public interface CustomFuctionInterface2 {
? ? String printStr();
}
代碼測(cè)試
@Test
public void test2() {
? ? CustomFuctionInterface2 customFuctionInterface2 = () -> "hello world";
? ? String printStr = customFuctionInterface2.printStr();
? ? System.out.println("printStr = " + printStr);
}
控制臺(tái)輸出結(jié)果:
演示三、實(shí)際項(xiàng)目中可借鑒使用(落地)
/**
?* @Title CustomFuctionInterface
?* @Description 自定義函數(shù)式接口之演示三、實(shí)際項(xiàng)目中可借鑒使用(落地)
?* @author calvin
?* @date: 2020/3/3 1:25 AM
?*/
@FunctionalInterface
public interface CustomFuctionInterface3 {
? ? void doSomething();
}
假設(shè)現(xiàn)在某個(gè)類(lèi)的某個(gè)方法形參為CustomFuctionInterface3,如代碼所示:
public static void execute(CustomFuctionInterface3 interface3) {
? ? interface3.doSomething();
}
傳統(tǒng)的調(diào)用方法 :
@Test
public void test3() {
? ? execute(new CustomFuctionInterface3() {
? ? ? ? @Override
? ? ? ? public void doSomething() {
? ? ? ? ? ? System.out.println("doSomething...");
? ? ? ? }
? ? });
}
控制臺(tái)輸出結(jié)果:
通過(guò)Lambda表達(dá)式改進(jìn)以上測(cè)試代碼:
@Test
public void test3() {
? ? execute(() -> System.out.println("doSomething..."));
}
再次查看控制臺(tái)輸出結(jié)果:
可以發(fā)現(xiàn)結(jié)果是一致的,代碼看起來(lái)更加簡(jiǎn)潔美觀。
總結(jié): 以上幾個(gè)小案例,通過(guò)使用函數(shù)式接口@FunctionalInterface+Lambda表達(dá)式進(jìn)行了代碼演示,對(duì)于我們理解函數(shù)式編程的思想以及并發(fā)工具包JUC下的函數(shù)型、斷定型、消費(fèi)者、供給型等接口的源碼閱讀都有一定的幫助。
————————————————
版權(quán)聲明:本文為CSDN博主「電商技術(shù)進(jìn)階」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/haogexiang9700/article/details/104623782
總結(jié)
以上是生活随笔為你收集整理的函数式接口@FunctionalInterface使用示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何解决分布式系统中的“幽灵复现”?-转
- 下一篇: 为何采用双亲委派机制