文章目錄
- 方法引入
- 什麼是方法
- 方法引用
- MayiktService
- MessageInterface
- MessageInterface2
- MessageInterface3
- 靜态方法引入
- 執行個體對象引用
- 構造函數引入
- 對象方法引入
方法引入
什麼是方法
方法引用:需要結合lambda表達式能夠讓代碼變得更加精簡。
- 匿名内部類使用
- Lambda調用匿名内部類
- 方法引入
方法引用
- 靜态方法引入: 類名::(靜态)方法名稱
- 對象方法引入 類名:: 執行個體方法名稱
- 執行個體方法引入 new對象 對象執行個體::方法引入
- 構造函數引入 類名::new
需要遵循一個規範:
方法引入 方法參數清單、傳回類型與函數接口參數清單與傳回類型必須要保持一緻
類型 | 文法 | 對應lambda表達式 |
構造器引用 | Class::new | (args) -> new 類名(args) |
靜态方法引用 | Class::static_method | (args) -> 類名.static_method(args) |
對象方法引用 | Class::method | (inst,args) -> 類名.method(args) |
執行個體方法引用 | instance::method | (args) -> instance.method(args) |
方法引用提供了非常有用的文法, 可以直接引用已有的Java類或對象的方法或構造器, 方法引用其實也離不開lambda表達式
- 構造器引用
- 靜态引用
- 對象方法引用
- 執行個體方法引用
MayiktService
public interface MayiktService {
String get(Test23 test23);
}
MessageInterface
@FunctionalInterface
public interface MessageInterface {
void get(Integer var1, Integer var2);
}
MessageInterface2
@FunctionalInterface
public interface MessageInterface2 {
String getMessage();
}
MessageInterface3
@FunctionalInterface
public interface MessageInterface3 {
MessageEntity getMessage();
}
靜态方法引入
public class TestLL01 {
public static void main(String[] args) {
//最原生的調用
// new MessageInterface() {
// @Override
// public String get(Integer var1, Integer var2) {
// System.out.println(ssss);;
// }
// }
//方法引入
MessageInterface staticGet = TestLL01::staticGet;
staticGet.get(12, 20);
}
public static void staticGet(Integer a, Integer b) {
System.out.println("staticGet,a:" + a + "+++" + b);
}
}
執行個體對象引用
public class TestLL02 {
/**
* 執行個體對象引用
*
* @param args
*/
public static void main(String[] args) {
// MessageInterface2 messageInterface2 = () -> {
// return "dasdasd";
// };
// System.out.println(messageInterface2.getMessage());
TestLL02 testLL02 = new TestLL02();
String object = testLL02.object();
System.out.println(object);
}
public String object() {
return "dadasda";
}
}
構造函數引入
public class TestLL03 {
/**
* 構造函數引用
*
* @param args
*/
public static void main(String[] args) {
MessageInterface3 messageInterface3 = () -> new MessageEntity();
AcanthopanaxInterface aNew = MessageEntity::new;
System.out.println(aNew);
}
}
對象方法引入
public class Test23 {
public static void main(String[] args) {
//1. 使用匿名内部類的形式
MayiktService mayiktService = new MayiktService() {
@Override
public String get(Test23 test23) {
return test23.object();
}
};
System.out.println(mayiktService.get(new Test23()));
//2.lambda
MayiktService mayiktService2 = test23 -> test23.object();
System.out.println(mayiktService2.get(new Test23()));
//3.方法引用 這哥時候函數接口 第一個參數傳遞test23 傳回調用test23.object方法
MayiktService mayiktService3 = Test23::object;
System.out.println(mayiktService3.get(new Test23()));
// R apply(T t); T applu方法傳遞的參數類型 : R apply 方法傳回的類型
//需要降string 類型字元串擷取長度
// Function<String, Integer> stringIntegerFunction = (str) -> {
// return str.length();
// };
Function<String, Integer> stringIntegerFunction1 = String::length;
System.out.println(stringIntegerFunction1.apply("dasdadasd"));
}
private String object() {
return "niwwewe";
}
}