zoukankan      html  css  js  c++  java
  • 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);
        }

    忘记了 我发完工资后还能去财务领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);
        }

  • 相关阅读:
    多进程编程
    Python 的下载安装
    cnBlogs windows LIves Writes 安装
    第四章网页文字编排设计
    第三章网页图形图像设计
    第二章网页创意设计思维和方法
    1.3-1.4网页设计的定位和流程
    1.2网页设计的构成要素和特性
    网页编辑常用快捷方式+学习技巧+网站开发流程
    css选择器2——伪类选择器
  • 原文地址:https://www.cnblogs.com/zhvip/p/12831013.html
Copyright © 2011-2022 走看看