进阶day06-Consumer的andThen方法
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package com.inmind.functional_interface02;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/*
|
||||
学习内容:
|
||||
default Consumer<T> andThen(Consumer after) 返回一个组成的 Consumer ,依次执行此操作,然后执行 after操作
|
||||
|
||||
需求:我要使用消费者接口,对同一个字符串进行大写,和小写的消费操作
|
||||
分析:
|
||||
1.一个消费者的消费操作:对字符串进行小写打印
|
||||
2.另一个消费者的消费操作:对字符串进行大写打印
|
||||
*/
|
||||
public class Demo06 {
|
||||
public static void main(String[] args) {
|
||||
String str = "aBcD";
|
||||
|
||||
print(
|
||||
s1-> System.out.println(s1.toLowerCase()),
|
||||
s2-> System.out.println(s2.toUpperCase()),
|
||||
str
|
||||
);
|
||||
}
|
||||
|
||||
public static void print(Consumer<String> consumer1, Consumer<String> consumer2, String s) {
|
||||
/*consumer1.accept(s);
|
||||
consumer2.accept(s);*/
|
||||
|
||||
/*
|
||||
andThen的源码:
|
||||
default Consumer<T> andThen(Consumer<? super T> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (T t) -> { accept(t); after.accept(t); };
|
||||
}
|
||||
|
||||
andThen:让多个消费者按顺序去消费同一个参数
|
||||
*/
|
||||
consumer1.andThen(consumer2).andThen(consumer2).accept(s);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user