Java 8 供应商在构造函数中带有参数 [英] Java 8 Supplier with arguments in the constructor

查看:27
本文介绍了Java 8 供应商在构造函数中带有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么供应商只支持无参数构造函数?

Why do suppliers only support no-arg constructors?

如果存在默认构造函数,我可以这样做:

If the default constructor is present, I can do this:

create(Foo::new)

但是如果唯一的构造函数接受一个字符串,我必须这样做:

But if the only constructor takes a String, I have to do this:

create(() -> new Foo("hello"))

推荐答案

这只是方法引用语法的一个限制——您不能传入任何参数.这就是语法的工作原理.

That's just a limitation of the method reference syntax -- that you can't pass in any of the arguments. It's just how the syntax works.

这篇关于Java 8 供应商在构造函数中带有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆