s-day07- 常用的函数式接口-Function

This commit is contained in:
2026-08-08 14:50:21 +08:00
parent b349f046e8
commit 5e936672fd
@@ -0,0 +1,32 @@
package com.inmind.functional_function06;
import java.util.function.Function;
/*
常用的函数式接口-Function,Function就是一个数据工厂,用来进行数据转换
Interface Function<T,R>
参数类型
T - 原料的类型 - 参数
R - 产品的类型 - 返回值
抽象方法:
R apply(T t):表示进T类型的数据转换成R类型的数据
默认方法:
default <V> Function<T,V> andThen(Function after) 合并2个Function接口,生成一个新的,先执行this的apply方法,再执行after的apply方法
default <V> Function<V,R> compose(Function before) 与以上的方法完全相反
需求:"100"->100
*/
public class Demo11 {
public static void main(String[] args) {
// method(s->{return Integer.parseInt(s);},"100");
method(s->Integer.parseInt(s),"100");
}
public static void method(Function<String, Integer> function, String s) {
Integer result = function.apply(s);
System.out.println("结果:"+ result);
}
}