类的静态方法引用
// 8.使用匿名内部类的方式通过函数式接口Function中的方法实现Integer类中parseInt方法的调用
Function<String, Integer> function = new Function<String, Integer>() {
@Override
public Integer apply(String s) {
return Integer.parseInt(s);
}
};
System.out.println(function.apply("12345")); // 12345
Function<String, Integer> function1 = s -> Integer.parseInt(s);
System.out.println(function1.apply("12345")); // 12345
Function<String, Integer> function2 = Integer::parseInt;
System.out.println(function2.apply("12345")); // 12345
- 类的静态方法引用 ClassName :: StaticMethodName
- parseInt方法是有参有返回值的,所以我们用Function,泛型<String,Integer>String表示传入的参数类型,Integer表示返回的参数类型,泛型不支持基本数据类型,所以我们这里需要使用基本数据类型的包装类Integer
- 类名Integer 静态方法parseInt