最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

javaLambda表達(dá)式

2022-09-16 08:55 作者:虛云幻仙  | 我要投稿

/**
* Lambda表達(dá)式用于簡(jiǎn)化代碼
* 函數(shù)式接口:只有一個(gè)抽象方法的接口,可以有其他默認(rèn)方法
* Lambda表達(dá)式可用于簡(jiǎn)化匿名內(nèi)部類的代碼結(jié)構(gòu)
*/

@FunctionalInterface
interface NoReturn0Parameter{
? ?//包含沒(méi)有返回值,沒(méi)有參數(shù)的抽象方法的函數(shù)式接口
? ?//在接口上方用注釋@FunctionalInterface 來(lái)限制接口內(nèi)只能有一個(gè)抽象方法,當(dāng)有多個(gè)時(shí)報(bào)錯(cuò)

? ?void method();
? ?//抽象方法:返回值類型 方法名()
}
@FunctionalInterface
interface NR1P{
? ?//無(wú)返回1參數(shù)
? ?void method(int a);
}
@FunctionalInterface
interface NR2P{
? ?//無(wú)返回2參
? ?void method(int a,int b);
}
@FunctionalInterface
interface R0P{
? ?//有返回值無(wú)參
? ?int method();
}
@FunctionalInterface
interface R1P{
? ?//有返回1參
? ?int method(int a);
}
@FunctionalInterface
interface R2P{
? ?//有返回2參
? ?int method(int a,int b);
}
public class TestLambda1 {
? ?public static void main(String[] args) {
? ? ? ?NoReturn0Parameter noReturn0Parameter = new NoReturn0Parameter() {
? ? ? ? ? ?//匿名內(nèi)部類通常寫法
? ? ? ? ? ?@Override
? ? ? ? ? ?public void method() {
? ? ? ? ? ? ? ?System.out.println("TestLambda1.method");
? ? ? ? ? ?}
? ? ? ?};

? ? ? ?NoReturn0Parameter noReturn0Parameter1 = ()->{
? ? ? ? ? ?System.out.println("TestLambda1.main");
? ? ? ?};
? ? ? ?//Lambda表達(dá)式格式: (參數(shù))->{方法體}
? ? ? ?//無(wú)參所以用(),{}方法體不用寫聲明的部分(public void method),因?yàn)楹瘮?shù)式接口必須只有一個(gè)抽象方法,所以lambda實(shí)現(xiàn)的就是這個(gè)方法,所以省略了聲明

? ? ? ?noReturn0Parameter1.method();
? ? ? ?//noReturn0Parameter1是實(shí)現(xiàn)類對(duì)象,引用類型是NoReturn0Parameter接口,實(shí)現(xiàn)了方法后可正常調(diào)用
? ? ? ?NR1P nr1P = (int a)->{
? ? ? ? ? ?System.out.println(a);
? ? ? ?};
? ? ? ?nr1P.method(20);
? ? ? ?NR2P nr2P = (int a,int b)->{
? ? ? ? ? ?System.out.println(a+b);
? ? ? ?};
? ? ? ?nr2P.method(20,30);

? ? ? ?R0P r0P = ()->{
? ? ? ? ? ?System.out.println("TestLambda1.main");
? ? ? ? ? ?return 10;
? ? ? ?};
? ? ? ?//有返回值則在方法體內(nèi)寫明
? ? ? ?int c = r0P.method();
? ? ? ?R1P r1P = (int a) -> {
? ? ? ? ? ?System.out.println("TestLambda1.main");
? ? ? ? ? ?return a;
? ? ? ?};
? ? ? ?R2P r2P = (int a, int b)->{
? ? ? ? ? ?System.out.println("TestLambda1.main");
? ? ? ? ? ?return a+b;
? ? ? ?};
? ?}
}
class TestLambda2{
? ?public static void main(String[] args) {
? ? ? ?//簡(jiǎn)寫Lambda,()->{}是完整形式,代碼可以再簡(jiǎn)化
? ? ? ?NoReturn0Parameter noReturn0Parameter = ()-> System.out.println("TestLambda2.main");
? ? ? ?//方法體{}只有一行代碼時(shí)可以去掉{}
? ? ? ?NR1P nr1P = a-> System.out.println(a);
? ? ? ?//參數(shù)()只有一個(gè)參數(shù)時(shí)可以省略(),參數(shù)可以省略類型
? ? ? ?NR2P nr2P = (a,b)-> System.out.println(a+b);
? ? ? ?//只有在參數(shù)為一個(gè)時(shí)可以省略(),但可以省略類型
? ? ? ?R0P r0P = ()->20;
? ? ? ?//當(dāng)方法體只有一句return 20 時(shí)將{}和return都省略
? ? ? ?R1P r1P = a->{
? ? ? ? ? ?System.out.println("TestLambda2.main");
? ? ? ? ? ?return a;
? ? ? ?};
? ? ? ?//當(dāng)方法體有多行代碼時(shí){}和return都不能省略
? ? ? ?R2P r2P = (a,b)->a+b;
? ? ? ?//將{return a+b}簡(jiǎn)寫為a+b

? ? ? ?//閉包問(wèn)題
? ? ? ?int x = 20;
? ? ? ?NoReturn0Parameter noReturn0Parameter1 = new NoReturn0Parameter() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void method() {
? ? ? ? ? ? ? ?System.out.println(x);
? ? ? ? ? ?}
? ? ? ?};
? ? ? ?//匿名內(nèi)部類為閉包的一種,在閉包中調(diào)用外部的變量,會(huì)將變量賦final屬性,后續(xù)再對(duì)x進(jìn)行更改的話內(nèi)部類的x會(huì)報(bào)錯(cuò),如果在內(nèi)部類之前變量x就已經(jīng)多次賦值了,在閉包中調(diào)用x會(huì)直接報(bào)錯(cuò),lambda表達(dá)式簡(jiǎn)化匿名內(nèi)部類代碼,在表達(dá)式內(nèi)調(diào)用外部變量同樣會(huì)賦final屬性
? ? ? ?//x++;報(bào)錯(cuò)

? ?}
}
class TestLambda3{
? ?//在Lambda中引入其他方法
? ?public static void print(int a){
? ? ? ?System.out.println(a);
? ?}
? ?public int times(int a,int b){
? ? ? ?return a*b;
? ?}

? ?public static void main(String[] args) {
? ? ? ?NR1P nr1P = a->TestLambda3.print(a);
? ? ? ?//使用已存在的方法來(lái)實(shí)現(xiàn)抽象方法
? ? ? ?nr1P = TestLambda3::print;
? ? ? ?//簡(jiǎn)寫為 方法歸屬者::方法名
? ? ? ?//要求引入的方法和抽象方法的參數(shù)列表和返回值類型必須一致,而因?yàn)閰?shù)和返回值一致所以將()參數(shù)和return都省略了,->goes to運(yùn)算符也省略了,而print靜態(tài)方法屬于類TestLambda3,所以寫為類名::方法名

? ? ? ?R2P r2P = new TestLambda3()::times;
? ? ? ?//times方法屬于對(duì)象,所以引用時(shí)需要寫為 對(duì)象::方法名
? ? ? ?NR1P nr1P1 = System.out::println;
? ? ? ?//將 a->System.out.println(a);簡(jiǎn)寫 ?println是屬于對(duì)象out的
? ? ? ?R2P r2P1 = Integer::sum;
? ? ? ?//將 (a,b)->a+b; 改寫為調(diào)用Integer.sum(int a,int b)方法
? ? ? ?//反過(guò)來(lái)說(shuō),將Integer.sum(int a,int b)方法的.換成:: 再去掉參數(shù)(int a,int b),寫成Integer::sum


? ? ? ?//使用Lambda表達(dá)式創(chuàng)建線程
? ? ? ?Thread t1 = new Thread(new Runnable() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void run() {
? ? ? ? ? ? ? ?System.out.println("通常的匿名內(nèi)部類");
? ? ? ? ? ?}
? ? ? ?},"匿名內(nèi)部類線程");
? ? ? ?Thread t2 = new Thread(()->{
? ? ? ? ? ?System.out.println("因?yàn)閚ew Thread()構(gòu)造器中聲明了參數(shù)類型為Runnable,Lambda表達(dá)式將引用類型變量 Runnable xxx= 省略,只寫表達(dá)式的部分");
? ? ? ? ? ?System.out.println("Runnable接口內(nèi)只有一個(gè)抽象方法run(),沒(méi)有其他任何內(nèi)容,即Runnable接口為函數(shù)式接口,適用Lambda");
? ? ? ?});

? ?}
}

javaLambda表達(dá)式的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
宣威市| 临海市| 屯留县| 怀化市| 招远市| 德钦县| 泰安市| 扎囊县| 永胜县| 临西县| 嘉禾县| 巨野县| 秦皇岛市| 镇原县| 平湖市| 河北区| 咸丰县| 西充县| 洛扎县| 博乐市| 罗平县| 平舆县| 武平县| 大悟县| 礼泉县| 新晃| 吴堡县| 潜江市| 道孚县| 汶川县| 武城县| 上饶县| 龙里县| 尉氏县| 乌兰浩特市| 黑河市| 沙洋县| 舞阳县| 元朗区| 闸北区| 正镶白旗|