代码:
package com.xxx.xxx.utils;import com.alibaba.fastjson.JSON; import lombok.Data;import java.util.function.BiConsumer; import java.util.function.Function;/*** 函数测试*/ public class Test {@Datapublic static class TestObj {private String name;private String nameContent;}/*** 获取数据函数*/public static final Function<TestObj, String> get = TestObj::getName;/*** 插入数据函数*/public static final BiConsumer<TestObj, String> set = TestObj::setNameContent;/*** 处理数据方法** @param t 实例对象* @param get 对象的get方法函数* @param set 对象的set方法函数* @param <T> T*/public static <T> void addContentFun(T t,Function<T, String> get,BiConsumer<T, String> set) {String name = get.apply(t);set.accept(t, name + ": 大家好,我是" + name);}/*** test*/public static void main(String[] args) {// 创建测试对象TestObj obj = new TestObj();obj.setName("zhang san");// 调用处理方法Test.addContentFun(obj, Test.get, Test.set);System.out.println("end data: " + JSON.toJSONString(obj));// end data: {"name":"zhang san","nameContent":"zhang san: 大家好,我是zhang san"}} }