加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Consumer和Begin方法如何在java功能接口内部工作

发布时间:2020-12-15 05:14:27 所属栏目:Java 来源:网络整理
导读:谁可以让我知道以下程序如何在内部工作: public class Main { public static void main(String[] args) { ConsumerString c = (x) - System.out.println(x.toLowerCase()); c.andThen(c).andThen(c).accept("Java2s.com"); }} 解决方法 文档 Consumer#andTh
谁可以让我知道以下程序如何在内部工作:

public class Main {
  public static void main(String[] args) {
    Consumer<String> c = (x) -> System.out.println(x.toLowerCase());
    c.andThen(c).andThen(c).accept("Java2s.com");
  }
}

解决方法

文档 Consumer#andThen说:

Returns a composed Consumer that performs,in sequence,this operation followed by the 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 after operation will not be performed.

这意味着您的消费者将被接受3次,如:

v--- 1. c.accept("Java2s.com");        
c.andThen(c).andThen(c).accept("Java2s.com");
          ^          ^      
          |          |           
          |  3.  c.accept("Java2s.com");               
          |
2. c.accept("Java2s.com");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读