溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何理解Java中的構(gòu)造函數(shù)引用和方法引用

發(fā)布時(shí)間:2021-10-13 15:44:43 來(lái)源:億速云 閱讀:120 作者:柒染 欄目:編程語(yǔ)言

如何理解Java中的構(gòu)造函數(shù)引用和方法引用,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

JDK 8 見(jiàn)證了一個(gè)特殊特性的出現(xiàn):構(gòu)造函數(shù)引用和方法引用。 Adrian D. Finlay 探討了開發(fā)人員如何釋放構(gòu)造函數(shù)引用的真正潛力。

方法引用的一些背景

如果你還不知道 Java 構(gòu)造函數(shù)本身就是特殊的方法,那么閱讀方法引用的基本示例將對(duì)讀者有所幫助,通過(guò)了解這些內(nèi)容,可以了解構(gòu)造函數(shù)引用是什么。

「方法引用為已經(jīng)有名稱的方法提供易讀的 lambda 表達(dá)式?!?/p>

「它們提供了一種無(wú)需執(zhí)行就可以引用方法的簡(jiǎn)單方式?!?/p>

以上引自《Java 8 編程參考官方教程(第 9 版)》,作者:Herbert Schildt

方法引用可以引用靜態(tài)方法和實(shí)例方法,兩者是通用的。方法引用是函數(shù)式接口的實(shí)例。雖然 Lambda 表達(dá)式允許你動(dòng)態(tài)創(chuàng)建方法實(shí)現(xiàn),但通常情況下,一個(gè)方法最終會(huì)調(diào)用 Lambda 表達(dá)式中的另一個(gè)方法來(lái)完成我們想要完成的工作。更直接的方法是使用方法引用。當(dāng)你已經(jīng)有一個(gè)方法來(lái)實(shí)現(xiàn)這個(gè)函數(shù)式接口時(shí),這是非常有用的。

讓我們看一個(gè)使用靜態(tài)方法及實(shí)例方法的示例。

//step #1 - Create a funnctional interface.interface FuncInt {//contains one and only abstract methodString answer(String x, boolean y);}//step #2 - Class providing method(s)that match FuncInt.answer()'s definition.class Answer {static String ans_math_static(String x, Boolean y) {return "\"" + x + "\"" + "\t = \t" + y.toString().toUpperCase();}String ans_math_inst(String x, Boolean y) {return "\"" + x + "\"" + "\t = \t" + y.toString().toUpperCase();}}

譯注:以上代碼的測(cè)試用例如下,因靜態(tài)方法與實(shí)例方法結(jié)果相同,僅以靜態(tài)方法為例。

Answer.ans_math_static("9 > 11 ?", false);Answer.ans_math_static("987.6 < 1.1 ?", false);Answer.ans_math_static("1 > 0.9 ?", true);Answer.ans_math_static("T/F: Is Chengdu in Sichuan?", true);Answer.ans_math_static("-1 % 0.2=0 ?", false);Answer.ans_math_static("T/F: Does Dwyne Wade play for the Knicks?", false);

得到與原文舉例相同的輸出結(jié)果:

"9 > 11 ?" = FALSE"987.6 < 1.1 ?" = FALSE"1 > 0.9 ?" = TRUE"T/F: Is Chengdu in Sichuan?" = TRUE"-1 % 0.2=0 ?" = FALSE"T/F: Does Dwyne Wade play for the Knicks?" = FALSE

使用方法引用的主要步驟有:

1.定義一個(gè)函數(shù)式接口

2.定義一個(gè)滿足函數(shù)式接口抽象方法要求的方法

3.使用對(duì)步驟2中定義的 (x :: y ) 方法引用實(shí)例化函數(shù)式接口的實(shí)例。

譯注:靜態(tài)方法的方法引用格式為 類名 :: 方法名 ;實(shí)例方法的方法引用格式為 對(duì)象實(shí)例名 :: 方法名 。

4.使用函數(shù)式接口實(shí)例調(diào)用方法: Instance.AbstractMethod();

這提供了一種創(chuàng)建方法實(shí)現(xiàn)的可插拔方式。Lambda 表達(dá)式和方法引用為 Java 編程帶來(lái)了一個(gè)功能方面的提升。

構(gòu)造函數(shù)的方法引用

讓我們開始詳細(xì)討論吧。

構(gòu)造函數(shù)和其他方法一樣是方法。對(duì)嗎?錯(cuò)。它們有點(diǎn)特殊,它們是對(duì)象初始化方法。盡管如此,它們?nèi)匀皇且粋€(gè)方法,沒(méi)有什么能阻止我們像其他方法引用一樣創(chuàng)建構(gòu)造函數(shù)的方法引用。

//step #1 - Create a funnctional interface.interface FuncInt {//contains one and only abstract methodAutomobile auto(String make, String model, short year);}//step #2 - Class providing method(s)that match FuncInt.answer()'s definition.class Automobile {//Trunk Member Variablesprivate String make;private String model;private short year;//Automobile Constructorpublic Automobile(String make, String model, short year) {this.make = make;this.model = model;this.year = year;}protected void what() {System.out.println("This Automobile is a" + year + " " + make + " " + model + ".");}}//Step #3 - Class making use of method referencepublic class ConstrRef {static void createInstance() {}public static void main(String[] args) {System.out.println();//Remember, a Method Reference is an instance of a Functional Interface. Therefore....FuncInt auto = Automobile::new;//We really don't gain much from this example//Example #1Automobile honda = auto.auto("honda", "Accord", (short) 2006);honda.what();//Example #1Automobile bmw = auto.auto("BMW", "530i", (short) 2006);bmw.what();System.out.println();}}

輸出結(jié)果

This Automobile is a2006 honda Accord.This Automobile is a2006 BMW 530i.

說(shuō)明

用戶應(yīng)該清楚的第一件事是這個(gè)基本示例沒(méi)有那么實(shí)用。這是一種相當(dāng)迂回的創(chuàng)建對(duì)象實(shí)例的方式。實(shí)際上,幾乎可以肯定,你不會(huì)經(jīng)歷所有這些麻煩來(lái)創(chuàng)建一個(gè) Automobile 實(shí)例,但是為了概念的完整性,還是要提及。

使用構(gòu)造函數(shù)的方法引用的主要步驟有:

1.定義一個(gè)只有抽象方法的函數(shù)式接口,該方法的返回類型與你打算使用該對(duì)象進(jìn)行構(gòu)造函數(shù)引用的對(duì)象相同。

2.創(chuàng)建一個(gè)類,該類的構(gòu)造函數(shù)與函數(shù)式接口的抽象方法匹配。

3.使用對(duì)步驟 #2 中定義的構(gòu)造函數(shù)的方法引用,實(shí)例化函數(shù)式接口的實(shí)例。

譯注:構(gòu)造函數(shù)的方法引用格式為 類名 :: new

4.在步驟 #2 中使用構(gòu)造函數(shù)引用實(shí)例化類的實(shí)例,例如 MyClass x = ConstructorReference.AbstractMethod (x, y, z…)

構(gòu)造函數(shù)引用與泛型一起使用的時(shí)候變得更有用。通過(guò)使用泛型工廠方法,可以創(chuàng)建各種類型的對(duì)象。

讓我們看一看。

//step #1 - Create a funnctional interface.interface FuncInt<Ob, X, Y, Z> {//contains one and only abstract methodOb func(X make, Y model, Z year);}//step #2 - Create a Generic class providing a constructor compatible with FunInt.func()'s definitionclass Automobile<X, Y, Z> {//Automobile Member Variablesprivate X make;private Y model;private Z year;//Automobile Constructorpublic Automobile(X make, Y model, Z year) {this.make = make;this.model = model;this.year = year;}protected void what() {System.out.println("This Automobile is a " + year + " " + make + " " + model + ".");}}//step #3 - Create a Non-Generic class providing a constructor compatible with FunInt.func()'s definitionclass Plane {//Automobile Member Variablesprivate String make;private String model;private int year;//Plane Constructorpublic Plane(String make, String model, int year) {this.make = make;this.model = model;this.year = year;//Automatic unboxing}protected void what() {System.out.println("This Plane is a " + year + " " + make + " " + model + ".");}}//Step #3 - Class making use of method reference with genericspublic class ConstrRefGen {//Here is where the magic happensstatic <Ob, X, Y, Z> Ob factory(FuncInt<Ob, X, Y, Z> obj, X p1, Y p2, Z p3) {return obj.func(p1, p2, p3);}public static void main(String[] args) {System.out.println();//Example #1FuncInt<Automobile<String, String, Integer>, String, String, Integer> auto_cons = Automobile<String, String, Integer>::new;Automobile<String, String, Integer> honda = factory(auto_cons, "Honda", "Accord", 2006);honda.what();//Example #2FuncInt<Plane, String, String, Integer> plane_cons = Plane::new;Plane cessna = factory(plane_cons, "Cessna", "Skyhawk", 172);cessna.what();System.out.println();}}

輸出結(jié)果

This Automobile is a 2006 Honda Accord.This Plane is a 172 Cessna Skyhawk.

說(shuō)明

這里有很多東西需要消化。事實(shí)上,如果你以前從未深入研究過(guò)泛型,那么這些代碼看上去可能相當(dāng)晦澀。讓我們分解一下。

我們做的第一件事是創(chuàng)建一個(gè)通用的函數(shù)式接口。注意細(xì)節(jié)。我們有四個(gè)泛型類型參數(shù):Ob、X、Y、Z。

Ob 代表要引用其構(gòu)造函數(shù)的類。  X,Y,Z 代表該類的構(gòu)造函數(shù)的參數(shù)。

如果我們替換泛型方法占位符,抽象方法可能是這樣的: SomeClass func (String make, String model, int year)。注意,由于我們使接口具有了泛型,所以可以指定任何返回類型或我們希望返回的類實(shí)例。這釋放了構(gòu)造函數(shù)引用的真正潛力。

接下來(lái)的兩個(gè)部分相對(duì)簡(jiǎn)單,我們創(chuàng)建了相同的類,一個(gè)泛型類和一個(gè)非泛型類,以演示它們與在公共類中定義的工廠方法的互操作性。注意,這些類的構(gòu)造函數(shù)與 FuncInt.func() 的方法簽名是兼容的。

進(jìn)入公共類的文件。這個(gè)方法就是奇跡發(fā)生的地方。

//Here is where the magic happensstatic <Ob, X, Y, Z> Ob factory(FuncInt<Ob, X, Y, Z> obj, X p1, Y p2, Z p3) {return obj.func(p1, p2, p3);}

我們將該方法標(biāo)記為靜態(tài)的,所以我們可以不使用 ConstRefGen 實(shí)例,畢竟它是一個(gè)工廠方法。注意,factory 方法具有與函數(shù)式接口相同的泛型類型參數(shù)。注意,方法的返回類型是 Ob,它可以是由我們決定的任何類。當(dāng)然,X、Y、Z是 Ob 中方法的方法參數(shù)。請(qǐng)注意,該函數(shù)以 FuncInt 的一個(gè)實(shí)例作為參數(shù)(類類型和方法參數(shù)作為類型參數(shù)),同時(shí)也接受 Ob 類型的類作為方法的參數(shù)。

在方法體中,它調(diào)用方法引用并將在 factory() 中傳遞的參數(shù)提供給它。

我們的第一個(gè)任務(wù)是創(chuàng)建一個(gè)符合 FuncInt<> 的方法引用。

這里我們分別引用 Automobile 類和 Plane 類的構(gòu)造函數(shù)。

我們的下一個(gè)任務(wù)是創(chuàng)建一個(gè)帶有方法引用的對(duì)象。

為此,我們調(diào)用 factory() 并將它需要的構(gòu)造函數(shù)引用以及 factory() 定義的有關(guān)構(gòu)造函數(shù)的參數(shù)提供給它。factory() 可以靈活地創(chuàng)建對(duì)各種方法的構(gòu)造函數(shù)引用,因?yàn)樗峭ㄓ玫?。因?yàn)?Plane 類和 Automobile 類的構(gòu)造函數(shù)匹配 FuncInt.func() 的方法簽名,所以它們可作為 FuncInt.func() 的方法引用使用。factory() 通過(guò)調(diào)用 obj.func(x,y,z) 返回類的一個(gè)實(shí)例,這是一個(gè)構(gòu)造函數(shù)方法引用,當(dāng)求值時(shí),它將為你提供指定為其參數(shù)的類的一個(gè)實(shí)例。

斟酌這個(gè)問(wèn)題一段時(shí)間,會(huì)發(fā)現(xiàn)它是Java的一個(gè)非常有用的補(bǔ)充 如何理解Java中的構(gòu)造函數(shù)引用和方法引用

看完上述內(nèi)容,你們掌握如何理解Java中的構(gòu)造函數(shù)引用和方法引用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI