1. 方法引用
1.1 體驗方法引用
代碼示例:
需求:
- 1:定義一個接口(Printable):裏面定義一個抽象方法: void printString(String s);
- 2:定義一個測試類(PrintableDemo),在測試類中提供兩個方法
一個方法是: usePrintable(Printable p)
一個方法是主方法,在主方法中調用usePrintable方法
接口:
//1:定義一個接口(Printable):裏面定義一個抽象方法: void printString(String s);
public interface Printable {
void printString(String s);
}
測試類:
//2:定義一個測試類(PrintableDemo),在測試類中提供兩個方法
//一個方法是: usePrintable(Printable p)
//一個方法是主方法,在主方法中調用usePrintable方法
public class PrintableDemo {
public static void main(String[] args) {
//Lambda表達式省略參數類型,是因爲方法中有參數類型這樣Lambda就可以推導出參數類型。
usePrintable(s -> System.out.println(s));
// 使用方法一引用,方法引用符::
//方法引用和Lambda一樣都是推導出了參數類型,然後將參數放入println中。
usePrintable(System.out::println);
}
private static void usePrintable(Printable p) {
p.printString("愛生活愛java");
}
}
運行結果:
1.2 方法引用符
代碼示例:
需求:
- 1:定義一個接口(Printable):裏面定義一個抽象方法: void printInt(int i);
- 2:定義一一個測試類(PrintableDemo),在測試類中提供兩個方法
一個方法是: usePrintable(Printable p)
一個方法是主方法,在主方法中調用usePrintable方法
定義接口:
//1:定義一個接口(Printable):裏面定義一個抽象方法: void printInt(int i);
public interface Printable {
void printInt(int i);
}
定義測試類:
//2:定義一一個測試類(PrintableDemo),在測試類中提供兩個方法
//一個方法是: usePrintable(Printable p)
//一個方法是主方法,在主方法中調用usePrintable方法
public class PrintableDemo {
private static void usePrintable(Printable p) {
p.printInt(666);
}
public static void main(String[] args) {
//Lambda表達式
usePrintable(i -> System.out.println(i));
//方法引用
usePrintable(System.out::println);
}
}
運行結果:
1.3 Lambda表達式支持的方法引用
1.4 引用類方法
代碼示例:
定義接口:
//1:定義一個接口(Converter),裏面定義一個抽象方法:
//int convert(String s);
public interface Converter {
int convert(String s);
}
定義測試類:
//2:定義一一個測試類(ConverterDemo),在測試類中提供兩個方法
//一個方法是: useConverter(Converter c)
//一個方法是主方法,在主方法中調用useConverter方法
public class ConverterDemo {
private static void useConverter(Converter c) {
int number = c.convert("666");
System.out.println(number);
}
public static void main(String[] args) {
// Lambda表達式
// 帶有返回值類型的也可以省略
useConverter(s -> Integer.parseInt(s));
// 引用類方法,採用引用類方法格式
useConverter(Integer::parseInt);
// Lambda表達式被類方法替代的時候,它的形式參數全部傳遞給靜態方法作爲參數
}
}
運行結果:
1.5 引用對象的實例方法
代碼示例:
定義類:
//1:定義一個類(PrintString),裏面定義一個方法
// public void printUpper(String s): 把字符串參數變成大寫的數據,然後在控制檯輸出
public class PrintString {
public void printUpper(String s) {
String result = s.toUpperCase();
System.out.println(result);
}
}
定義接口:
//2:定義一個接口(Printer),裏面定義一個抽象方法
//void printUpperCase(String s);
public interface Printer {
void printUpperCase(String s);
}
定義測試類:
//3:定義一個測試類(PrinterDemo),在測試類中提供兩個方法
//一個方法是: usePrinter(Printer p)
//一個方法是主方法,在主方法中調用usePrinter方法
public class PrinterDemo {
private static void usePrinter(Printer p) {
p.printUpperCase("HelloWordl");
}
public static void main(String[] args) {
// Lambda表達式,最簡化模式
usePrinter(s -> System.out.println(s.toUpperCase()));
// 引用對象的是實例方法
PrintString ps = new PrintString();
usePrinter(ps::printUpper);
// Lambda表達式被對象的實例方法替代的時候,它的形式參數全部傳遞給該方法作爲參數
}
}
運行結果:
1.6 引用類的實例方法
代碼示例:
定義接口:
//1:定義一個接口(MyString),裏面定義一個抽象方法: .
//String mySubString(String s,int x, int y);
public interface MyString {
String mySubString(String s, int x, int y);
}
定義測試類:
//2:定義一個測試類(MyStringDemo),在測試類中提供兩個方法
//一個方法是: useMyString(MyString my)
//一個方法是主方法,在主方法中調用useMyString方法
public class MyStringDemo {
private static void useMyString(MyString my) {
String s = my.mySubString("HelloWorld", 2, 5);
System.out.println(s);
}
public static void main(String[] args) {
// Lambda表達式,最簡模式
useMyString((s, x, y) -> s.substring(x, y));
// 引用類的實例方法
useMyString(String::substring);
// Lambda表達式被類的實例方法替代的時候
// 第一個參數作爲調用者
// 後面的參數全部傳遞給該方法作爲參數
}
}
運行結果:
1.7 引用構造器(構造方法)
代碼示例:
定義類Student
//1:定義一個類(Student),裏面有兩個成員變量(name, age)
//並提供無參構造方法和帶參構造方法,以及成員變量對應的get和set方法
public class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
定義接口:
//2:定義一個接口(StudentBuilder),裏面定義一個抽象方法
//Student build(String name, int age);
public interface StudentBuilder {
Student build(String name, int age);
}
定義測試類:
//3:定義一個測試類(StudentDemo),在測試類中提供兩個方法
//一個方法是: useStudentBuilder(StudentBuilder s)
//一個方法是主方法,在主方法中調用useStudentBuilder方法
public class StudentDemo {
private static void useStudentBuilder(StudentBuilder sb) {
Student s = sb.build("林青霞", 20);
System.out.println(s.getName() + "," + s.getAge());
}
public static void main(String[] args) {
// Lambda表達式
useStudentBuilder((name, age) -> new Student(name, age));
// 引用構造器
useStudentBuilder(Student::new);
// L ambda表達式被構造器替代的時候,它的形式參數全部傳遞給構造器作爲參數
}
}
運行結果: