
Function<String, String> f1 = String::toUpperCase;
System.out.println(f1.apply("ab"));
Function<String, Boolean> f0 = String::matches;
System.out.println(f0.apply("aa"));
String類中的toUpperCase和matches都不是static方法,為啥“String::toUpperCase”能正常使用?
uj5u.com熱心網友回復:
::是一層語法糖,跟靜態方法沒關系,理解也很簡單
Function<String, String> f1 = String::toUpperCase;
System.out.println(f1.apply("ab"));
實際等價于
System.out.println("ab".toLowerCase());
但是第二個function
Function<String, Boolean> f0 = String::matches;
System.out.println(f0.apply("aa"));
你能等價于什么呢,引數都對應不上
System.out.println("aa".matches(????));
很明顯這個方法你不能用::來寫
uj5u.com熱心網友回復:
是 :: 只能三在無引數的方法上嘛?在哪里能找到 :: 的實作邏輯(原始碼?)轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/276049.html
標籤:Java EE
下一篇:idea springMVC href="${pageContext.request.contextPath} Cannot resolve file
