BiFunction接口笔记

今天学习到了BiFunction这个函数接口,就地取材想到了我们公司发工资的这个应用场景。

@FunctionalInterface
public interface BiFunction<T, U, R>

 

T入参

U入参

R返回值

    /**
     * Applies this function to the given arguments.
     *
     * @param t the first function argument
     * @param u the second function argument
     * @return the function result
     */
    R apply(T t, U u);

 

将复杂的问题简单化、抽象化

我们公司的工资是简单的根据出勤天数、绩效核算出我们的工资,实际上当然复杂很多,哈哈哈~

    public static void main(String[] args) {

        JavaTest javaTest = new JavaTest();
        System.out.println("这个月工资发了:"+ javaTest.payoff(22,90,(workDay,performance)->{

            //基本工资  实际出勤天数/正常出勤参数
            float baseMoney = 3000 * (workDay / 22.0f);
            //绩效 100分的绩效,大家一般都只能得90分左右
            float performanceMoney = 3000 * (performance / 100.f);

            return (int) (baseMoney + performanceMoney );
        }));

    }

    public int payoff(int workDay, int performance,BiFunction<Integer,Integer,Integer> function){
        return function.apply(workDay,performance);
    }

 

BiFunction接口笔记

 

忘记了 我发完工资后还能去财务领50块钱的话费补贴。还好有这个方法

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     */
    default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }

 

先调用 BiFunction的原函数,再调用after函数,先发工资(打卡上)再领话费补贴(发现金)

    public static void main(String[] args) {

        JavaTest javaTest = new JavaTest();
        System.out.println("这个月工资发了:"+ javaTest.payoff(22,90,(workDay,performance)->{

            //基本工资 * 出勤天数
            float baseMoney = 3000 * (workDay / 22.0f);
            //绩效 100分的绩效,大家一般都只能得90分左右
            float performanceMoney = 3000 * (performance / 100.f);

            return (int) (baseMoney + performanceMoney );
        }));

        System.out.println("这个月加上补贴一共发了:" + javaTest.payoff1(22,90,(workDay,performance)->{
            //基本工资 * 出勤天数
            float baseMoney = 3000 * (workDay / 22.0f);
            //绩效 100分的绩效,大家一般都只能得90分左右
            float performanceMoney = 3000 * (performance / 100.f);

            return (int) (baseMoney + performanceMoney );
        },money -> money+ 50));

    }

    public int payoff(int workDay, int performance,BiFunction<Integer,Integer,Integer> function){
        return function.apply(workDay,performance);
    }


    public int payoff1(int workDay, int performance,BiFunction<Integer,Integer,Integer> function,Function<Integer,Integer>function2) {
        return function.andThen(function2).apply(workDay, performance);
    }

BiFunction接口笔记

上一篇:Google插件Performance工具进行性能分析


下一篇:performance_schema初相识