具有参数的Java 8供应商在构造函数中 [英] Java 8 Supplier with arguments in the constructor
本文介绍了具有参数的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屋!
查看全文