A lambda expression can be understood as a concise representation of an anonymous function
that can be passed around: it doesn’t have a name, but it has a list of parameters, a body, a
return type, and also possibly a list of exceptions that can be thrown. That’s one big definition;
#格式:
(x, y) -> x y;
-----------------
(x,y):參數(shù)列表
x y : body
package LambdaUse;
import java.net.Inet4Address;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* @author zhangwenlong
* @date 2019/8/25 16:53
*/
public class paramterMethod {
public static void main(String[] args) {
//情況1:A method reference to a static method (for example, the method parseInt of Integer, written Integer::parseInt)
//靜態(tài)方法
//常用的使用
Integer value = Integer.parseInt("123");
System.out.println(value);
//使用方法推導(dǎo)
Function<String,Integer> stringIntegerFunction = Integer::parseInt;
Integer apply = stringIntegerFunction.apply("123");
System.out.println(apply);
//情況2:A method reference to an instance method of an arbitrary type (for example, the method length of a String, written String::length)
//對象的方法
String ss = "helloWorld";
System.out.println(ss.charAt(3));
//推導(dǎo)方法
BiFunction<String,Integer,Character> stringIntegerCharacterBiFunction = String::charAt;
System.out.println(stringIntegerCharacterBiFunction.apply(ss,3));
//情況3:構(gòu)造函數(shù)方法推導(dǎo)
//常用方式
Apple apple = new Apple("蘋果",120);
System.out.println(apple);
//推導(dǎo)方式
BiFunction<String,Integer,Apple> appleBiFunction = Apple::new;
System.out.println(appleBiFunction.apply("蘋果",120));
}
}
執(zhí)行結(jié)果:
123
123
l
l
Apple{name='蘋果', weight=120}
Apple{name='蘋果', weight=120}