supplier也是是用来创建对象的,但是不同于传统的创建对象语法:new,看下面代码:
public class TestSupplier {
private int age;
(www.0831jlyy.com)
TestSupplier(){
System.out.println(age);
}
public static void main(String[] args) {
//创建Supplier容器,声明为TestSupplier类型,此时并不会调用对象的构造方法,即不会创建对象
Supplier<TestSupplier> sup= TestSupplier::new;
System.out.println("--------");
//调用get()方法,此时会调用对象的构造方法,即获得到真正对象
sup.get();
//每次get都会调用构造方法,即获取的对象不同
sup.get();
}
}
输出结果:

--------
0
0
官方代码及注释:
/**(m.jlnk3659999.com)
* Represents a supplier of results.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html" rel="external nofollow" >functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
(3g.xcjl0834.com)
/**
* Gets a result.
*
* @return a result
*/
T get();
}
根据代码和官方注释,我的个人理解:
1.supplier是个接口,有一个get()方法
2.语法 :
Supplier<TestSupplier> sup= TestSupplier::new;
3.每次调用get()方法时都会调用构造方法创建一个新对象。

最新文章

  1. [小北De编程手记] : Lesson 05 玩转 xUnit.Net 之 从Assert谈UT框架实践
  2. jax-rs中的一些参数标注简介(@PathParam,@QueryParam,@MatrixParam,@HeaderParam,@FormParam,@CookieParam)
  3. 欧洲杯 2016 高清直播 - 观看工具 UEFA-EURO-2016-Play.7z
  4. jquery事件代理
  5. 万象客户端设置服务端ip保存在注册表的位置
  6. mysql传统主从、双主复制+keepalived配置步骤
  7. Jetty提交数据时报java.lang.IllegalStateException: Form too large270468&gt;200000问题解决
  8. Java 8 Lambda表达式
  9. Java自动装箱拆箱
  10. SQL Server表分区【转】
  11. windows“画图”工具用法
  12. java 变长參数使用原则
  13. java的WindowBuilder可视化插件
  14. 使用缓存Memcache存储access_token
  15. Hibernate验证器
  16. 饮冰三年-人工智能-Python-30 python开发中常见的错误
  17. [是男人就过8题——Pony.ai]Perfect N-P Arrays
  18. 阿里云oss,简单上传
  19. CMake--常用指令
  20. TiKV 源码解析系列文章(三)Prometheus(上)

热门文章

  1. rest framework之路由组件
  2. android studio 设计任务内容和识别内容界面 (android stuido design task layout)
  3. PHP FILTER_UNSAFE_RAW 过滤器
  4. PHP ftp_chdir() 函数
  5. java中获取类加载路径和项目根路径的5种方式分析
  6. python爬虫 mac下安装使用Fiddler
  7. unittest框架学习笔记一之testcase
  8. spring中的web上下文,spring上下文,springmvc上下文区别(超详细)
  9. Mysql 导入日文数据乱码问题
  10. centos安装vbox addition