lambda方法引用

引用静态方法
...
ackage edu.xcdq;

interface IMyMessage<P,R>{
R int2String(P zhengshu);
}

public class Demo04 {
public static void main(String[] args) {

    IMyMessage<Integer,String>shili = String::valueOf;
    String str = shili.int2String(50000);
    System.out.println(str.length());
}

}

...
引用某个对象的方法
...
package edu.xcdq;
//引用 对性的方法
interface IMyMessage1<R>{
R zhuandaxie();
}
public class Demo05 {
public static void main(String[] args) {

    IMyMessage1<String> shili = "hello "::toUpperCase;
    System.out.println(shili.zhuandaxie());
}

}

...
引用某个特定类的方法
...
package edu.xcdq;
interface IMyMessage6<R,P>{
R compare(P p1, P p2);
}
public class Demo06 {
public static void main(String[] args) {
IMyMessage6<Integer,String>message6 = String::compareTo;
System.out.println(message6.compare("侯","姚"));
}
}
...
引用构造方法
...
package edu.xcdq;
//引用构造方法 Student::new
interface IMyPerson<R,PN,PA>{
R message(PN name,PA age);
}
public class Demo07 {
public static void main(String[] args) {

    IMyPerson<Student,String,Integer>shili = Student::new;
    System.out.println(shili.message("Jackma",50));
}

}
...

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容