zoukankan      html  css  js  c++  java
  • java8 Consumer

    @FunctionalInterface
    public interface Consumer<T> {
    
        /**
         * Performs this operation on the given argument.
         *
         * @param t the input argument
         */
        void accept(T t);
    
        /**
         * Returns a composed {@code Consumer} that performs, in sequence, this
         * operation followed by the {@code after} operation. If performing either
         * operation throws an exception, it is relayed to the caller of the
         * composed operation.  If performing this operation throws an exception,
         * the {@code after} operation will not be performed.
         *
         * @param after the operation to perform after this operation
         * @return a composed {@code Consumer} that performs in sequence this
         * operation followed by the {@code after} operation
         * @throws NullPointerException if {@code after} is null
         */
        default Consumer<T> andThen(Consumer<? super T> after) {
            Objects.requireNonNull(after);
            return (T t) -> { accept(t); after.accept(t); };
        }
    }

    Consumer可以改变入参的内部状态

    //结合Consumer可以让其他对象直间接用lamaba表达式

    //案例1
    Student A=new Student('AA');
    Consumer<Student> B=e -> Student.addName('BB');
    B.accept(A);//这时A中的Name值为BB

    //案例2
    test(1,e->{System.out.println(e);}); public static <T> void test(Int e, Consumer<? super T> c) { c.accept(e); }
  • 相关阅读:
    【halcon】学习记录
    【QT】常用类
    【QT】宏
    机器视觉名词解释
    单元测试
    【MFC】VS2017新建完MFC后,没有界面,只有代码
    【MFC】学习与问题整合
    函数重载(overload)和函数重写(override)
    工作记录+反思
    【转】MapReduce:默认Counter的含义
  • 原文地址:https://www.cnblogs.com/Babylon/p/9178958.html
Copyright © 2011-2022 走看看