From c2452d081161990d11a82fe88c3c05aa08828f17 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sat, 14 Mar 2026 13:16:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E9=98=B6day06-=E5=B8=B8=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=E5=BC=8F=E6=8E=A5=E5=8F=A3-Function?= =?UTF-8?q?(=E8=BD=AC=E6=8D=A2=E5=B7=A5=E5=8E=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inmind/functional_interface02/Demo13.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 javaSE-day07/src/com/inmind/functional_interface02/Demo13.java diff --git a/javaSE-day07/src/com/inmind/functional_interface02/Demo13.java b/javaSE-day07/src/com/inmind/functional_interface02/Demo13.java new file mode 100644 index 0000000..e968f0c --- /dev/null +++ b/javaSE-day07/src/com/inmind/functional_interface02/Demo13.java @@ -0,0 +1,37 @@ +package com.inmind.functional_interface02; + +import java.util.function.Function; + +/* +学习的内容: +6.常用的函数式接口-Function,Function就是一个数据工厂,用来进行数据转换 + Interface Function +参数类型 + T - 原料的类型 - 参数 + R - 产品的类型 - 返回值 + 抽象方法: + R apply(T t):表示进T类型的数据转换成R类型的数据 + 默认方法: + default Function andThen(Function after) 合并2个Function接口,生成一个新的,先执行this的apply方法,再执行after的apply方法 + default Function compose(Function before) 与以上的方法完全相反 + + 需求:"100"->100 + */ +public class Demo13 { + public static void main(String[] args) { +// method(str->Integer.parseInt(str),"100"); +// method((String str)->{return Integer.parseInt(str);},"100"); + //----------------------------------也可以这么做----------------------------- + //定义一个转换工厂 + Function function = str->Integer.parseInt(str);//接口的多态 + Integer result = function.apply("10"); + System.out.println(result); + + } + + //定义出一个方法,接收的是一个数据工厂(String->Integer),字符串参数,没有返回值 + public static void method(Functionfunction,String str) { + Integer result = function.apply(str); + System.out.println(result); + } +}