Consumer是消费性接口,通用的方法是用accept()方法;
使用方法一:
1 public static void main(String[] args) { 2 Consumer<String> consumer = content -> { 3 System.out.println(content); 4 }; 5 consumer.accept("hello, world"); 6 }
使用方法二:
1 private static void doAction(String s) { 2 System.out.println(s); 3 } 4 5 public static void main(String[] args) { 6 Consumer<String> consumer = TestConsumer::doAction; 7 consumer.accept("hello, world"); 8 }