欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年06月05日 10:03:31   作者:jaxenter  
java構(gòu)造函數(shù),也叫構(gòu)造方法,是java中一種特殊的函數(shù)。函數(shù)名與相同,無(wú)返回值。方法引用是用來(lái)直接訪問(wèn)類(lèi)或者實(shí)例的已經(jīng)存在的方法或者構(gòu)造方法。下面我們來(lái)詳細(xì)了解一下它們吧

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

方法引用的一些背景

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

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

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

以上引自《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 method
String 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)方法的方法引用格式為 類(lè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ù)的方法引用

讓我們開(kāi)始詳細(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 method
Automobile auto(String make, String model, short year);
}
//step #2 - Class providing method(s)that match FuncInt.answer()'s definition.
class Automobile {
//Trunk Member Variables
private String make;
private String model;
private short year;
//Automobile Constructor
public 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 reference
public 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 #1
Automobile honda = auto.auto("honda", "Accord", (short) 2006);
honda.what();
//Example #1
Automobile 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ō)明

用戶(hù)應(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ù)式接口,該方法的返回類(lèi)型與你打算使用該對(duì)象進(jìn)行構(gòu)造函數(shù)引用的對(duì)象相同。

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

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

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

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

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

讓我們看一看。

//step #1 - Create a funnctional interface.
interface FuncInt<Ob, X, Y, Z> {
//contains one and only abstract method
Ob func(X make, Y model, Z year);
}
//step #2 - Create a Generic class providing a constructor compatible with FunInt.func()'s definition
class Automobile<X, Y, Z> {
//Automobile Member Variables
private X make;
private Y model;
private Z year;
//Automobile Constructor
public 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 definition
class Plane {
//Automobile Member Variables
private String make;
private String model;
private int year;
//Plane Constructor
public 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 generics
public class ConstrRefGen {
//Here is where the magic happens
static <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 #1
FuncInt<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 #2
FuncInt<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è)泛型類(lèi)型參數(shù):Ob、X、Y、Z。

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

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

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

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

//Here is where the magic happens
static <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ù)式接口相同的泛型類(lèi)型參數(shù)。注意,方法的返回類(lèi)型是 Ob,它可以是由我們決定的任何類(lèi)。當(dāng)然,X、Y、Z是 Ob 中方法的方法參數(shù)。請(qǐng)注意,該函數(shù)以 FuncInt 的一個(gè)實(shí)例作為參數(shù)(類(lèi)類(lèi)型和方法參數(shù)作為類(lèi)型參數(shù)),同時(shí)也接受 Ob 類(lèi)型的類(lèi)作為方法的參數(shù)。

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

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

這里我們分別引用 Automobile 類(lèi)和 Plane 類(lèi)的構(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 類(lèi)和 Automobile 類(lèi)的構(gòu)造函數(shù)匹配 FuncInt.func() 的方法簽名,所以它們可作為 FuncInt.func() 的方法引用使用。factory() 通過(guò)調(diào)用 obj.func(x,y,z) 返回類(lèi)的一個(gè)實(shí)例,這是一個(gè)構(gòu)造函數(shù)方法引用,當(dāng)求值時(shí),它將為你提供指定為其參數(shù)的類(lèi)的一個(gè)實(shí)例。

斟酌這個(gè)問(wèn)題一段時(shí)間,會(huì)發(fā)現(xiàn)它是Java的一個(gè)非常有用的補(bǔ)充 ;)

英文鏈接: jaxenter

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?NIO實(shí)現(xiàn)聊天室功能

    Java?NIO實(shí)現(xiàn)聊天室功能

    這篇文章主要為大家詳細(xì)介紹了Java?NIO實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享功能

    多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享功能

    這篇文章主要介紹了多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java基于注解的Excel導(dǎo)出方式

    Java基于注解的Excel導(dǎo)出方式

    這篇文章主要介紹了Java基于注解的Excel導(dǎo)出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • java并發(fā)之synchronized

    java并發(fā)之synchronized

    這篇文章主要介紹了java并發(fā)關(guān)鍵字synchronized,包括內(nèi)容synchronized的使用、synchronized背后的Monitor、synchronized保證可見(jiàn)性和防重排序、使用synchronized注意嵌套鎖定,具體內(nèi)容請(qǐng)看下面文章吧
    2021-10-10
  • spring-boot-autoconfigure模塊用法詳解

    spring-boot-autoconfigure模塊用法詳解

    autoconfigure就是自動(dòng)配置的意思,spring-boot通過(guò)spring-boot-autoconfigure體現(xiàn)了"約定優(yōu)于配置"這一設(shè)計(jì)原則,而spring-boot-autoconfigure主要用到了spring.factories和幾個(gè)常用的注解條件來(lái)實(shí)現(xiàn)自動(dòng)配置,思路很清晰也很簡(jiǎn)單,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • Java8 Optional的詳細(xì)使用教程

    Java8 Optional的詳細(xì)使用教程

    這篇文章主要給大家介紹了關(guān)于Java8 Optional的詳細(xì)使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Java多線程并發(fā)編程 并發(fā)三大要素

    Java多線程并發(fā)編程 并發(fā)三大要素

    這篇文章主要介紹了Java多線程并發(fā)編程 并發(fā)三大要素,需要的朋友可以參考下
    2017-05-05
  • java中實(shí)現(xiàn)對(duì)象排序的兩種方法(Comparable,Comparator)

    java中實(shí)現(xiàn)對(duì)象排序的兩種方法(Comparable,Comparator)

    這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)對(duì)象排序的兩種方法,一種是實(shí)現(xiàn)Comparable進(jìn)行排序,另一種是實(shí)現(xiàn)Comparator進(jìn)行排序,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • Java中的Lombok使用及工作原理詳解

    Java中的Lombok使用及工作原理詳解

    這篇文章主要介紹了Java中的Lombok使用及工作原理詳解,Lombok是一個(gè)Java庫(kù),能自動(dòng)插入編輯器并構(gòu)建工具,簡(jiǎn)化Java開(kāi)發(fā),通過(guò)添加注解的方式,不需要為類(lèi)編寫(xiě)getter或eques方法,同時(shí)可以自動(dòng)化日志變量,需要的朋友可以參考下
    2023-10-10
  • SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式

    SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式

    這篇文章主要介紹了SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評(píng)論