?? ?? ??: java ?? ????
1. Lambda ??? ??
Lambda ???? Java8? ??? ???? Java8?? ?? ?? ?? ??? ?? ? ?????. (? ?? ??? ??? ???? ????????.)
Lambda ???? ?????
?? ???
???. ? ?? ???? ??????????? ???? ??
? ? ????.匿名方法
??梢允褂眠@個匿名方法,實現接口中的方法
。功能:通常使用Lambda表達式,是為了
簡化接口實現
的。關于接口實現可以有多種方式實現,例如:①設計接口的實現類、②使用匿名內部類。但是③使用lambda表達式,比這兩種方式都簡單。要求:lambda表達式,
只能實現函數式接口
:即一個接口中,要求實現類必須實現的抽象方法,有且只有一個。
@FunctionalInterface注解
,用在接口之前,用來判斷接口是否是一個函數式接口。如果不是函數式接口會報錯。功能類似于@Override。
二、Lambda表達式語法
lambda表達式本質上是一個匿名方法,因此再寫lambda表達式時,不需要關心方法名是什么,也不需要關心返回值類型。只需要關心兩部分:
參數列表
、方法體
。
- ()參數部分:方法的參數列表,要求和實現的接口中的方法參數部分一致,包括參數的數量和類型。
- {}方法體部分:方法的實現部分,如果接口中定義的方法有返回值,則在實現時,注意返回值的返回。
- -> :分隔參數部分和方法體部分。
Lambda表達式基礎語法:(參數)?->{ 方法體}
下面定義6種參數和返回值各不相同的
函數式接口
,分別使用lambda表達式對接口中的方法進行實現:
下面是針對上面6種函數式接口的lambda表達式實現。
/** ?*?@Description: ?*?@author?Guoqianliang ?*?@date?19:50?-?2021/2/15 ?*/public?class?BasicSyntax?{ ????public?static?void?main(String[]?args)?{ ????????//?1.實現無參數,無返回值的函數式接口 ????????NoneReturnNoneParameter?lambda1?=?()?->?{ ????????????System.out.println("這是無參,無返回值的方法"); ????????}; ????????lambda1.test(); ????????//?2.實現一個參數,無返回值的函數式接口 ????????NoneReturnSingleParameter?lambda2?=?(int?a)?->?{ ????????????System.out.println("這是一個參數,無返回值的方法,參數a:"?+?a); ????????}; ????????lambda2.test(10); ????????//?3.實現多個參數,無返回值的函數式接口 ????????NoneReturnMutipleParameter?lambda3?=?(int?a,?int?b)?->?{ ????????????System.out.println("這是多個參數,無返回值的方法,參數a="?+?a?+?",b="?+?b); ????????}; ????????lambda3.test(10,?20); ????????//?4.實現無參數,有返回值有返回值的函數式接口 ????????SingleReturnNoneParameter?lambda4?=?()?->?{ ????????????System.out.println("這是無參數,有返回值的方法,返回值是:"); ????????????return?10; ????????}; ????????System.out.println(lambda4.test()); ????????//?5.實現一個參數,有返回值的函數式接口 ????????SingleReturnSingleParameter?lambda5?=?(int?a)?->?{ ????????????System.out.println("這是一個參數,有返回值的方法,返回值是:"); ????????????return?a; ????????}; ????????System.out.println(lambda5.test(10)); ????????//?6.實現多個參數,有返回值的函數式接口 ????????SingleReturnMutipleParameter?lambda6?=?(int?a,?int?b)?->?{ ????????????System.out.println("這是多個參數,有返回值的方法,返回值是:"); ????????????return?a?+?b; ????????}; ????????System.out.println(lambda6.test(1,?2)); ????}}
語法精簡進階:
- 參數列表的參數類型可以省略。
- 如果參數列表中的參數有且只有一個,可以省略小括號。
- 如果方法體中只有一條語句,可以省略大括號。(注:如果這條語句是返回語句,省略了大括號后也要把return關鍵字省略)
三、函數引用
lambda表達式是為了簡化接口。在lambda表達式中,不應該出現比較復雜的邏輯。如果需要處理的邏輯比較復雜,一般情況會單獨寫一個方法。在lambda表達式中直接引用這個方法即可。即
引用一個已經存在的方法,使其代替lambda表達式完成接口的實現。
1.靜態(tài)方法引用
語法:
類::靜態(tài)方法
- 在引用的方法后面,不要添加小括號。
- 引用的這個方法,參數(數量、類型)和返回值,必須要跟接口中定義的一致。
/** ?*?@Description:?方法引用 ?*?@author?Guoqianliang ?*?@date?0:26?-?2021/2/16 ?*/public?class?Lambda1?{ ????private?static?interface?Calculate?{ ????????int?calculate(int?a,?int?b); ????} ????private?static?int?calculate(int?x,?int?y)?{ ????????if?(x?>?y)?{ ????????????return?x?-?y; ????????}?else?if?(x?<p><strong>2.非靜態(tài)方法引用</strong></p><blockquote> <p>語法:<code>對象::非靜態(tài)方法</code></p> <ul> <li>在引用的方法后面,不要添加小括號。</li> <li>引用的這個方法,參數(數量、類型)和返回值,必須要跟接口中定義的一致。</li> </ul> </blockquote><pre class="brush:php;toolbar:false">/** ?*?@Description:?方法引用 ?*?@author?Guoqianliang ?*?@date?0:26?-?2021/2/16 ?*/public?class?Lambda1?{ ????private?static?interface?Calculate?{ ????????int?calculate(int?a,?int?b); ????} ????//?非靜態(tài)方法 ????private?int?calculate2(int?a,?int?b)?{ ????????if?(a?!=?b)?{ ????????????return?a?-?b; ????????} ????????return?a?+?b; ????} ????public?static?void?main(String[]?args)?{ ????????//?非靜態(tài)方法引用 ????????Calculate?calculate2?=?new?Lambda1()::calculate2; ????????System.out.println(calculate.calculate(10,?20)); ????}}
3.構造方法引用
語法:
類名::new
- 可以通過接口中的方法的參數,區(qū)分引用不同的構造方法。
- 如果某一個函數式接口中定義的方法,僅僅是為了得到一個類的對象。此時就可以使用構造方法的引用,簡化這個方法的實現。
/** ?*?@Description:?構造方法引用 ?*?@author?Guoqianliang ?*?@date?11:20?-?2021/2/16 ?*/public?class?Lambda2?{ ????@FunctionalInterface ????private?interface?GetPersonWithNoneParameter?{ ????????Person?get(); ????} ????@FunctionalInterface ????private?interface?GetPersonWithSingleParameter?{ ????????Person?get(String?name); ????} ????@FunctionalInterface ????private?interface?GetPersonWithMutipleParameter?{ ????????Person?get(String?name,?int?age); ????} ????private?static?class?Person?{ ????????String?name; ????????int?age; ????????public?Person()?{ ????????????System.out.println("Person類的無參構造方法執(zhí)行了"); ????????} ????????public?Person(String?name)?{ ????????????this.name?=?name; ????????????System.out.println("Person類的有參構造方法執(zhí)行了"); ????????} ????????public?Person(String?name,?int?age)?{ ????????????this.name?=?name; ????????????this.age?=?age; ????????????System.out.println("Person類的兩個參數的構造方法執(zhí)行了"); ????????} ????} ????public?static?void?main(String[]?args)?{ ????????//?1.使用lambda表達式,實現GetPersonWithNoneParameter接口 ????????GetPersonWithNoneParameter?getPerson?=?Person::new; ????????//?2.使用lambda表達式,實現GetPersonWithSingleParameter接口 ????????GetPersonWithSingleParameter?getPerson2?=?Person::new; ????????//?3.使用lambda表達式,實現GetPersonWithMutipleParameter接口 ????????GetPersonWithMutipleParameter?getPerson3?=?Person::new; ????????System.out.println(getPerson.get()); ????????System.out.println(getPerson2.get("樹先生")); ????????System.out.println(getPerson3.get("你好",?23)); ????}}
4.對象方法的特殊引用
使用lambda表達式實現某些接口時,如果lambda表達式中包含了某一個對象,此時方法體中,直接使用這個對象調用它的某一個方法就可以完成整體的邏輯。
/** ?*?@Description:?對象方法的特殊應用 ?*?@author?Guoqianliang ?*?@date?11:54?-?2021/2/16 ?*/public?class?Lambda3?{ ????@FunctionalInterface ????private?interface?MyInterface?{ ????????//?String?get(Person?person); ????????void?set(Person?person,?String?name); ????} ????private?static?class?Person?{ ????????private?String?name; ????????public?void?setName(String?name)?{ ????????????this.name?=?name; ????????} ????????public?String?getName()?{ ????????????return?name; ????????} ????} ????public?static?void?main(String[]?args)?{ ????????Person?p1?=?new?Person(); ????????p1.setName("小明");//????????邏輯實現只是為了獲取到對象的名字//????????MyInterface?lambda2?=?Person::getName;//????????System.out.println(lambda2.get(p1)); ???????? ????????//?邏輯實現只是為了給對象的某些屬性進行賦值 ????????MyInterface?lambda1?=?(x,?n)?->?x.setName(n); ????????MyInterface?lambda2?=?Person::setName; ????????lambda2.set(p1,?"李華"); ????????System.out.println(p1.getName()); ????}}
四、Lambda表達式需要注意的問題
??: ?? ???? ?????如果用到
局部變量
????? ??? ???
?? ? ?????. ?????? ???? ???? ??? ?? ?? ??? ????. ① ?????? ?? ???? ??????. ② ?? ?? ???? ?????. ??? ③ ?? ???? ???? ?? ? ? ?? ???? ? ?????. 2. ?? ? ?? ?????????? ?? ????? ?? ?????? ?? ?? ??? ? ??? ???? ?? ? ??? ?? ? ??? ????.?? ??: ?? ?,
??? ?????? ??? ? ??
: ?, ??????? ?? ???? ???? ?? ?? ???? ??? ????.@FunctionalInterface ??
? ?????? ??? ??????? ???? ?? ????? ?? ?????. ??? ?????? ?? ?? ??? ?????. ??? @Override? ?????.
???? ??
? ??? ??
? ??? ?? ?? ???. ????????() ???? ??: ???? ???? ??? ????? ?? ??? ???? ??? ?????? ??? ???? ??? ???? ???. ??{}??? ?? ??: ???? ?? ?????. ?????? ??? ???? ?? ?? ?? ?? ?? ? ?? ?? ?????. ??-> : ???? ??? ??? ?? ??? ?????. /** ?*?@Description: ?*?@author?Guoqianliang ?*?@date?13:05?-?2021/2/16 ?*/public?class?Lambda4?{ ????public?static?void?main(String[]?args)?{ ????????//?1.定義一個局部變量 ????????int?x?=?10; ????????//?2.使用lambda表達式實現接口 ????????LambdaTest?lambda?=?()?->?{ ????????????System.out.println("x="?+?x); ????????}; ????????//?3.?無法修改常量x ????????//?x=20; ????}}@FunctionalInterfaceinterface?LambdaTest?{ ????void?test();}??????? ??? ????? ?? ?? ???? 6?? ???
??? ?????
? ???? ?? ?? ???? ?????? ???? ?????. ?????? 
? ?? ?? ???? ????? ??? ??? ? ??? ?? ???? ?????.
????????1. ?? ??? ?????????? ??: Class::static ???
????????? ??? ?? ??? ???? ???. ????? ???? ????(??, ??) ? ?? ?? ?????? ??? ?? ???? ???. ??rrreee????2. ??? ??? ?????????? ??: Object::non-static ???
????????? ??? ??? ???? ???. ?? ????? ???? ????(??, ??) ? ?? ?? ?????? ??? ?? ???? ???. ??rrreee????3. ??? ??? ?????????? ??: ??? ??::new
???????? ???? ????? ?? ?? ?? ??? ??? ? ????. ????? ?? ??. ????? ?????? ??? ???? ?? ???? ??? ???? ?? ????. ? ???? ??? ???? ?? ??? ???? ? ???? ??? ???? ? ????. ??rrreee????4. ?? ???? ?? ?? ???????????? ???? ???? ?? ?????? ??? ? ?? ???? ??? ???? ??? ??? ???? ? ??? ???? ???. ?? ??? ???? ?? ?? ??? ? ??? ?????. ????rrreee????4. ?? ??? ?? ? ??? ??????????? ??
? ???? ????? ??? ???? ?? ??? ? ????. ????rrreee???????? ?? ????: ??????java basics????????? ??? Java 8? ?? ??? ?? ??? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











C++?? Lambda ???? ???? ??? ???? ? ?? ??? ????. ?, try-catch ??? ???? ??? ????, catch ???? ??? ????? ?? ??????. std::function ??? ?? ??? ?????? try_emplace ???? Lambda ????? ??? ??? ? ????.

C++?? ???? ?? ??? ???? ? ?? ?? ????. ???? ????? ?? ????? ?? ??? ?????. ???? ????, ?? ???, ?? ??? ?? ??? ?????. ?? ???? ?? ??? ?????? ??? ??? ? ?? ??? ???? ?? ?? ???? ?????.

C++ ?? ??? ??????? ?? ?? ???? ???, ???, ?? ?? ?? ??? ? ???? ?????. ?? ??: ?? ?? ???? ?? ???? ??? ?? ????? ??? ID? ???? ? ??? ???? ?? ???? ?????.

Java8? 1? ? ??? ???? ?? minus() ???? ???? 1? ? ?? 1? ? ??? ?????. packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo09{publicstaticvoidmain(String[ ]args ){LocalDatetoday=LocalDate.now();LocalDatepreviousYear=today.minus(1,ChronoUni

C++ Lambda ???? ?? ?? ??? ???? ??? ???? ? ??? ?? ???? ?????. ??? [?? ??](????)->return-type{function-body}???. ?? ??? ??? ??? ?????. [=]? ???? ?? ?? ??? ??? ????, [&]? ???? ?? ?? ??? ??? ?????, [??1, ??2,...]? ???? ?? ??? ??? ? ????. ?? ???? ??? ???? ???? ? ??? ?? ?? ??? ?? ????.

C++??? Lambda ???? ?? ?? ??? ???? ?? ??? ???? ?? ? ????. ????? ???? ??? ????. ???? ??: std::function? ?? Lambda ???? ???? ?? ?? ??? ???? ??? ?????. ?? ? ??: std::function? ???? ?? ?? ???? ??? ? ?? ? ??? ?????. ?? ??: GUI ??? ???? ??? ?????, ???? ??? ?? ??? ??? ????, ?? ???? ?? ???? ??????.

C++?? ?? ??? ?? ?? ???? ???? ? ??? ????. ??? ??: ??? ???? ????. ??? ??: ?? ??? ?????. ? ? ??? ??? ??: ? ?? ??? ?? ??? ??? ? ????.

C++ ?? ???? ???? ?? ??? ???? ??? ?????? ?? ?? ?? ??? ???? ?? ?? ?????. ??? ??? ??? ??? ??? ?????. ??? ???? ??? ???? ??? ??????.
