java函数式编程之Supplier

来源:互联网 发布:美国mac电磁阀 编辑:程序博客网 时间:2024/06/03 07:07

描述:Supplier< T>接口没有入参,返回一个T类型的对象,类似工厂方法。

源码:

public interface Supplier<T> {    /**     * Gets a result.     *     * @return a result     */    T get();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

测试代码:

@Testpublic void test(){    Supplier<User> supplier = ()->new User();    User user = supplier.get();    logger.info(user.toString());    logger.info(supplier.get().toString());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

测试结果:

十二月 25, 2016 9:55:46 上午 com.zm.demo.SuppierTest test信息: com.zm.entity.User@1963006a十二月 25, 2016 9:55:46 上午 com.zm.demo.SuppierTest test信息: com.zm.entity.User@6d9c638
  • 1
  • 2
  • 3
  • 4

可以看出调用一次返回一个对象

原创粉丝点击