国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

? Java Java??? Java 8? ?? ??? ?? ??? ??

Java 8? ?? ??? ?? ??? ??

Feb 18, 2021 pm 06:09 PM
java8 lambda

Java 8? ?? ??? ?? ??? ??

?? ?? ??: java ?? ????

1. Lambda ??? ??

Lambda ???? Java8? ??? ???? Java8?? ?? ?? ?? ??? ?? ? ?????. (? ?? ??? ??? ???? ????????.)

  • Lambda ???? ????? ?? ??????. ? ?? ???? ???? ??????? ???? ??? ? ????. 匿名方法??梢允褂眠@個匿名方法,實現接口中的方法。

  • 功能:通常使用Lambda表達式,是為了簡化接口實現的。關于接口實現可以有多種方式實現,例如:①設計接口的實現類、②使用匿名內部類。但是③使用lambda表達式,比這兩種方式都簡單。

  • 要求:lambda表達式,只能實現函數式接口:即一個接口中,要求實現類必須實現的抽象方法,有且只有一個。

@FunctionalInterface注解 ,用在接口之前,用來判斷接口是否是一個函數式接口。如果不是函數式接口會報錯。功能類似于@Override。

二、Lambda表達式語法

lambda表達式本質上是一個匿名方法,因此再寫lambda表達式時,不需要關心方法名是什么,也不需要關心返回值類型。只需要關心兩部分:參數列表、方法體

  • ()參數部分:方法的參數列表,要求和實現的接口中的方法參數部分一致,包括參數的數量和類型。
  • {}方法體部分:方法的實現部分,如果接口中定義的方法有返回值,則在實現時,注意返回值的返回。
  • -> :分隔參數部分和方法體部分。
Lambda表達式基礎語法:(參數)?->{
	方法體}

下面定義6種參數和返回值各不相同的函數式接口,分別使用lambda表達式對接口中的方法進行實現:

Java 8? ?? ??? ?? ??? ??

下面是針對上面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表達式需要注意的問題

如果用到局部變量

??: ?? ???? ????? ????? ??? ????? ? ?????. ?????? ???? ???? ??? ?? ?? ??? ????. ① ?????? ?? ???? ??????. ② ?? ?? ???? ?????. ??? ③ ?? ???? ???? ?? ? ? ?? ???? ? ?????.

?? ??: ?? ?, ??? ?????? ??? ? ??: ?, ??????? ?? ???? ???? ?? ?? ???? ??? ????. @FunctionalInterface ??? ?????? ??? ??????? ???? ?? ????? ?? ?????. ??? ?????? ?? ?? ??? ?????. ??? @Override? ?????.

2. ?? ? ?? ?????????? ?? ????? ?? ?????? ?? ?? ??? ? ??? ???? ?? ? ??? ?? ? ??? ????. ???? ??? ??? ?? ? ??? ?? ?? ???. ????????() ???? ??: ???? ???? ??? ????? ?? ??? ???? ??? ?????? ??? ???? ??? ???? ???. ??{}??? ?? ??: ???? ?? ?????. ?????? ??? ???? ?? ?? ?? ?? ?? ? ?? ?? ?????. ??-> : ???? ??? ??? ?? ??? ?????.
/**
?*?@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?? ??? ??? ?????? ???? ?? ?? ???? ?????? ???? ?????. ?????? ??? ?? ?? ??????????? ?? 6A? ?? ????. ??? ?????? ?? ? ?????. ????rrreee???? ?? ???: ?????????? ??? ???? ??? ??? ? ????. ?????? ??? ????? ??? ?? ?? ??? ??? ? ????. ????? ??? ??? ??? ?? ?? ???? ??? ? ????. (??: ? ???? return ?? ?? ???? ??? ? return ???? ???? ???.)????3. ?? ??????????lambda ???? ?????? ????? ?? ????. ?? ???? ? ??? ??? ????? ? ???. ???? ?? ??? ????? ??? ?? ??? ???? ???? ?? ??????. ?? ??? ? ???? ?? ???? ???. ?, ? ?? ?? ???? ????? ??? ??? ? ??? ?? ???? ?????. ????????1. ?? ??? ?????????? ??: Class::static ???????????? ??? ?? ??? ???? ???. ????? ???? ????(??, ??) ? ?? ?? ?????? ??? ?? ???? ???. ??rrreee????2. ??? ??? ?????????? ??: Object::non-static ???????????? ??? ??? ???? ???. ?? ????? ???? ????(??, ??) ? ?? ?? ?????? ??? ?? ???? ???. ??rrreee????3. ??? ??? ?????????? ??: ??? ??::new???????? ???? ????? ?? ?? ?? ??? ??? ? ????. ????? ?? ??. ????? ?????? ??? ???? ?? ???? ??? ???? ?? ????. ? ???? ??? ???? ?? ??? ???? ? ???? ??? ???? ? ????. ??rrreee????4. ?? ???? ?? ?? ???????????? ???? ???? ?? ?????? ??? ? ?? ???? ??? ???? ??? ??? ???? ? ??? ???? ???. ?? ??? ???? ?? ?? ??? ? ??? ?????. ????rrreee????4. ?? ??? ?? ? ??? ??????????? ??? ???? ????? ??? ???? ?? ??? ? ????. ????rrreee???????? ?? ????: ??????java basics????????

? ??? Java 8? ?? ??? ?? ??? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

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

???

??? ??

?? ????
1787
16
Cakephp ????
1730
56
??? ????
1581
29
PHP ????
1448
31
???
?? ???? C++?? ??? ??? ?????? ?? ???? C++?? ??? ??? ?????? Apr 17, 2024 pm 12:42 PM

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

C++ ?? ????? ???? ??? ?????? C++ ?? ????? ???? ??? ?????? Apr 17, 2024 pm 06:15 PM

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

?? ??? ?????? C++ ?? ?? ???? ?? ??? ???? ?? ??? ?????? C++ ?? ?? ???? ?? ??? ???? Apr 17, 2024 pm 05:24 PM

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

Java 8?? 1? ? ?? 1? ?? ??? ??? ?????? Java 8?? 1? ? ?? 1? ?? ??? ??? ?????? Apr 26, 2023 am 09:22 AM

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++ ?? ????? ???? ???? ??? ?????? C++ ?? ????? ???? ???? ??? ?????? Jun 01, 2024 pm 05:50 PM

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

C++ ?? ?? ?? ???: ???? ?? ? ?? ?? ?? ?? ??? C++ ?? ?? ?? ???: ???? ?? ? ?? ?? ?? ?? ??? May 03, 2024 pm 12:12 PM

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

C++ ?? ???? ?? ??? ??? ?????? C++ ?? ???? ?? ??? ??? ?????? Apr 17, 2024 pm 04:39 PM

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

C++ ?? ???? ???? ?? ??? ???? ??? ?????? C++ ?? ???? ???? ?? ??? ???? ??? ?????? Apr 17, 2024 pm 12:36 PM

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

See all articles