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);
        }

  • 相关阅读:
    用属性封装 Session 及 VIewState 的存取
    正则表达式的一些重要概念
    通用权限的思路。只是一个简单的思路。
    IBATISNETNET 1.3 开发指南系列文章
    Serializable===net对象序列化
    使用Asp.Net构建安全网站
    用汽车售票系统谈数据库结构设计
    图文描述Vs2005制作WEB应用程序安装包的方法[E8.Net正式用户可以找我们获取全部代码参考]
    《基于.NET平台的分层架构实战》系列文章索引
    javascript中outerHTML innerHTML innerTEXT 三者的区别
  • 原文地址:https://www.cnblogs.com/zhvip/p/12831013.html
Copyright © 2011-2022 走看看