关于java:方法引用

体验办法援用

//定义一个接口
public interface Printable{
    void PrintString(String s);//接口中有且仅有一个形象办法 没有返回值
}
//定义一个办法用这个接口作为参数
private static void usePrintable (Printable p){
    p.PrintString(s:"爱生存");
}
//定义主办法,在主办法中调用usePrintable办法
//间接应用Lambda表达式进行改良
usePrintable( (String s) -> {
    sout(s)
} );
//优化
usePrintable( s -> sout(s) );

用办法援用符 :: 进行改良(两个冒号)

usePrintable(System.out::println);
//System.out是一个对象
//用::援用外面的println办法
//可推导的就是可省略的 间接把参数s传给了println办法

办法援用符

  • :: 该符号为援用运算符,而它所在的表达式被称为办法援用


//定义一个接口
public interface Printable{
    void PrintInt(int i);//接口中有且仅有一个形象办法 没有返回值
}
//定义一个办法用这个接口作为参数
private static void usePrintable (Printable p){
    p.PrintInt(i:666);
}
//定义主办法,在主办法中调用usePrintable办法
//间接应用Lambda表达式进行改良
usePrintable( i -> sout(i) );

//用办法援用改良
usePrintable(System.out::println);

Lambda表达式反对的办法援用

常见援用形式

  • 援用类办法
  • 援用对象的实例办法
  • 援用类的实例办法
  • 援用结构器

援用类办法

  • 格局:类名 :: 静态方法
  • 范例:Interger::parseInt
    Interger类的办法:public static int parseInt(String s)将此String转换为int类型数据

练习

//定义一个接口
public interface Converter{
    void PrintString(String s);//接口中有且仅有一个形象办法 没有返回值
}

//定义一个办法用这个接口作为参数
private static void useConverter (Converter c){
    int number = c.Converter(s:"666");
    sout(number);
}

//定义主办法,在主办法中调用useConverter办法
//间接应用Lambda表达式进行改良
useConverter( (String s) -> {
    return Integer.parseInt(s);
} );
//优化
useConverter( s -> Integer.parseInt(s); );

//用办法援用改良
useConverter(Integer::parseInt);


Lambda表达式被类办法代替的时候,它的形式参数全副传递给静态方法作为参数

援用对象的实例办法

援用对象的实例办法,其实就是援用类中的成员办法

  • 格局:对象 :: 成员办法
  • 范例:”Hello World”::toUppCase
    String类的办法:public String toUppCase()将此String转换

练习

//定义一个接口
public interface Printer{
    void printUpperCase(String s);//接口中有且仅有一个形象办法 没有返回值
}

//定义一个类,类外面有办法
public class PrintString{
    //把字符串参数变成大写的数据,而后再控制台输入
    public void printUpper(String s){
        String result = s.toUpperCase();
        sout(result);
    }
}

//定义一个办法用这个接口作为参数
private static void usePrinter (Printer p){
    p.printUpperCase(s:"HelloWorld");
}

//定义主办法,在主办法中调用usePrinter办法
//间接应用Lambda表达式进行改良
usePrinter( (String s) -> {
    String result = s.toUpperCase();
    sout(result);
} );
//优化
usePrinter( s -> sout(s.toUpperCase()) );

//用办法援用改良 曾经存在这么一个办法
PrintString ps = new PrintString();
usePrinter(ps::printUpper);


Lambda表达式被对象的实例办法代替的时候,它的形式参数全副传递给该办法作为参数

援用类的实例办法

援用类的实例办法,其实就是援用类中的成员办法

  • 格局:类名 :: 成员办法
  • 范例:String :: substring
    String类的办法:public String substring(int beginIndex,int endIndex)
    从beginIndex开始到endIndex完结,截取字符串,返回一个子串,子串的长度为endIndex-beginIndex

练习

//定义一个接口
public interface MyString{
    String mySubString(String s,int x,int y);//接口中有且仅有一个形象办法 没有返回值
}

//定义一个办法用这个接口作为参数
private static void useMyString (MyString my){
    String s = my.mySubString(s:"HelloWorld",x:2,y:5);
    sout(s);
}

//定义主办法,在主办法中调用useMyString 办法
//间接应用Lambda表达式进行改良
useMyString( (String s,int x,int y) -> {
    return s.subString(x,y);
} );
//优化
useMyString( (s,x,y) -> s.subString(x,y) );

//用办法援用改良 曾经存在这么一个办法 援用类中的实例办法
useMyString(String::subString);


Lambda表达式被类的实例办法代替的时候,它的第一个参数作为调用者,前面的参数全副传递给该办法作为参数

援用结构器

援用结构器,其实就是援用构造方法

  • 格局:类名 :: new
  • 范例:Student :: new

练习

定义一个类,外面有两个成员变量name,age,并提供无参构造方法和带参构造方法,以及成员变量对应的get set办法

//定义一个接口
public interface StudentBuilder{
    Student build(String name,int age);//接口中有且仅有一个形象办法 没有返回值
}

//定义一个办法用这个接口作为参数
private static void useStudentBuilder (StudentBuilder sb){
    Student s = sb.build(name:"林青霞",age:30)
    sout(s.getName()+","+s.getAge());
}

//定义主办法,在主办法中调用useStudentBuilder办法
//间接应用Lambda表达式进行改良
useStudentBuilder( (String name,int age) -> {
    Student s = new Student(name,age);
    return s;
    //等同于上面
    return new Student(name,age);
} );
//优化
useStudentBuilder( (name,age) -> new Student(name,age) );

//用办法援用改良 曾经存在这么一个办法 援用类中的实例办法
useStudentBuilder(Student::new);


Lambda表达式被结构器代替的时候,它的形式参数全副传递给结构器作为参数

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理