日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java uppercase方法_java-方法引用

發布時間:2024/4/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java uppercase方法_java-方法引用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*** 方法引用格式:

* 雙冒號:: 引用運算符,它所在的表達式被稱為方法引用。如果Lambda表達式

* 的函數方案已經存在于某個地方的實現中,

* ===》那么可以通過雙冒號來引用改方法作為Lambda表達式的代替者*/

例子:

public classDemo01Print {private static voidprintString(Printable p){

p.print("quan");

}public static voidmain(String[] args) {

printString((s)->{

System.out.println(s);

});/*** 表達式的目的:打印參數傳遞的字符串

* 把參數s,傳遞給System.out對象,調用out對象的pringln方法進行輸出

* System.out對象已經存在

* pringln方法已經存在

* =====》可以使用方法引用來優化表達式

* 就是使用system.out方法直接引用(調用)pringln方法。*/printString(System.out::println);

}

結果輸出:

quan

quan

通過對象名引用成員方法

最常見的一種用法,其實就是上面的做法:

/*** 通過對象名引用成員方法

* 前提:

* 對象名存在,方法名存在

* 就可以使用對象名來引用成員方法*/

例子:

1先創建對象類

public classMethodRefObject {public voidpringUpperCaseString(String s){

System.out.println(s.toUpperCase());

}

}

2創建函數式接口

@FunctionalInterfacepublic interfacePrintable {//定義字符串的抽象方法

voidprint(String s);

}

3測試方法:

public classDemo01MethodRefObject {private static voidpringString(Printable p){

p.print("quan");

}public static voidmain(String[] args) {

pringString((s)->{//創建MethodRefObject對象

MethodRefObject obj = newMethodRefObject();//調用對象里面的成員方法pringUpperCaseString把字符//串按照大寫輸出

obj.pringUpperCaseString(s);

});//使用方法引用優化,先創建MethodObject對象

MethodRefObject obj = newMethodRefObject();

pringString(obj::pringUpperCaseString);

}

}/*** re:

* QUAN

* QUAN

**/

通過類名稱引用靜態方法

/*** 通過類名引用靜態承焰方法

* 前提:

* 類已經存在,靜態成員方法存在

**/

例子:

public classDemo01StaticMethodReference {//定義一個方法,方法的參數傳遞要計算的整數和函數式接口Calcable

private static int method(intnum,Calcable c){returnc.calAbs(num);

}public static voidmain(String[] args) {//調用method方法

int i =method(-10,(n)->{//計算絕對值,使用的是Math類當中的靜態方法abs

returnMath.abs(n);

});

System.out.println(i);/***使用方法引用優化Lambda表達式

* Math類存在

* abs計算絕對值靜態方法存在*/

int i1 = method(-10,Math::abs);

System.out.println(i1);

}

}

通過supper引用父類的成員方法

存在子父關系

創建一個父類

//父類

public classHuman {public voidsayHello(){

System.out.println("say hello");

}

}

創建一個函數式接口

@FunctionalInterfacepublic interfaceGreetable {voidgreet();

}

創建一個子類:

public class Man extendsHuman{

@Overridepublic voidsayHello() {

System.out.println("hello,I'm Man");

}public voidmethod(Greetable g){

g.greet();

}public voidshow(){

method(()->{//創建父類

Human human = newHuman();

human.sayHello();

});/*** 存在子夫類關系,存在super關鍵字,代表父類

* 直接通過super調用父類成員方法*/method(()->{super.sayHello();

});/*** 通過super引用類的成員方法*/method(super::sayHello);

}public static voidmain(String[] args) {newMan().show();newMan().sayHello();

}

}

結果:

say hello

say hello

say hello

hello,I'm Man

通過this引用成員方法

this代表當前對象,如果需要引用的方法就是當前類中的成員方法。

可以使用this::成員方法來使用方法引用

定義一個接口

@FunctionalInterfacepublic interfaceRichable {voidbuy();

}

/*** 通過this引用奔雷成員方法*/

public classHusbard {public voidbuyHome(){

System.out.println("買房子");

}public voidmarry(Richable r){

r.buy();

}public voidsoHappy(){

marry(()->{//使用this成員方法

this.buyHome();

});/*** 使用方法引用優化Lambda表達式*/marry(this::buyHome);

}public static voidmain(String[] args) {newHusbard().soHappy();

}

}

類的構造器引用

構造器和類名完全一致,所以使用方式為:類名稱::new的格式表示。

定義一個函數式接口

/*** 定義一個創建Person對象的函數式接口*/@FunctionalInterfacepublic interfacePersonBuilder {//根據姓名創建Person對象返回

Person builderPerson(String name);

}

一個要被創建的類Person

public classPerson {privateString name;publicPerson() {

}publicPerson(String name) {this.name =name;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}

}

/*** 類的構造器引用*/

public classDemo {public static voidprintName(String name,PersonBuilder builder){

Person person=builder.builderPerson(name);

System.out.println(person.getName());

}public static voidmain(String[] args) {

printName("quan",(name)->{return newPerson(name);

});/*** 使用方法引用優化lambda表達*/printName("quan2",Person::new);

}

}//re:quan//quan2

數組構造器的引用

數組是Object的子類對象,同樣具有構造器,但是語法不太一樣

/*** 數組的構造器引用

* 數組::new

* 例子:int[]::new*/

創建一個接口

//創建數組的函數式接口

@FunctionalInterfacepublic interfaceArrayBuilder {//返回一個給定長度的int類型數組

int[] builderArray(intlength);

}

public classDemo1 {public static int[] createArray(intlength,ArrayBuilder ab){returnab.builderArray(length);

}public static voidmain(String[] args) {//調用create方法,傳遞數組長度和表達式

int[] lenint = createArray(12,(len)->{return new int[len];

});

System.out.println(lenint.length);//優化,int[]就是數組的構造函數名字

int[] lenint2 = createArray(15,int[]::new);

System.out.println(lenint2.length);

}

}//re:12//15

總結

以上是生活随笔為你收集整理的java uppercase方法_java-方法引用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。