package mydemo.day12.demo07Function;
import java.util.function.Function;
/*
练习:自定义函数模型拼接
题目
请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
String str = “赵丽颖,20”;
分析:
1. 将字符串截取数字年龄部分,得到字符串;
Function<String,String> "赵丽颖,20"->"20"
2. 将上一步的字符串转换成为int类型的数字;
Function<String,Integer> "20"->20
3. 将上一步的int数字累加100,得到结果int数字。
Function<Integer,Integer> 20->120
/
public class Demo03Practice {
/
定义一个方法
参数传递包含姓名和年龄的字符串
参数再传递3个Function接口用于类型转换
*/
public static int change(String s, Function<String,String> fun1,Function<String,Integer> fun2,Function<Integer,Integer> fun3){
////使用andThen方法把三个转换组合到一起
return fun1.andThen(fun2).andThen(fun3).apply(s);
}
public static void main(String[] args) {
//定义一个字符串
String s = "赵丽颖,20";
int num = change(s, (String s1) -> {
// 获取字符串中的数值字符
return s1.split(",")[1];
}, (String s2) -> {
// 将数值字符转换为整数类型
return Integer.parseInt(s2);
}, (Integer s3) -> {
// 将转换的整数类型加上100
return s3+100;
});
System.out.println(num);
}
}