English 中文(简体)
Java:为瞬时反对的目的建立供应商?
原标题:Java: Creating a Supplier for an Object from an instantiated Object?

我想从现有的瞬息万.目标中建立一个 Java供应商。 与此类似:

CustomObject customObject = new CustomObject();
Supplier.of(customObject)

然而,瓦贾不存在这种供应商的辛迪加,我想知道,是否有一个方便的解决办法。

我知道,我可以很容易地建立这样一个供应商:

Supplier<CustomObject> object = ()-> new CustomObject()
Supplier<CustomObject> object = CustomObject::new

然而,在我使用的情况下,我想从现有的惯例中抽取供应商,允许抽象。

这样做是否方便?

question 处理的方法略有不同。

问题回答

供应商不是这样创造的。 在标准JDK中不存在固定方法Supplier#of(......)

如果你想建立<条码> 补充<> 条码>,提供以前创建的例:

CustomObject customObject = new CustomObject();
Supplier<CustomObject> supplier = () -> customObject;

Though doing this, you lose the benefits of lazy instantiation as the instance becomes always instantiated before and regardless of whether the supplier is called or not.

最后,记住<条码>底线。 标记必须标明为<代码>final,或停留在“有效最终”上,即你可以调任任何其他东西。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签