List< Void>的目的是什么? [英] What is the purpose of List<Void>?
问题描述
我甚至不知道这是可行的,但是我在线阅读了一些代码时看到了这样的签名:
I didn't even know this was doable, but I saw while perusing some code online a method with a signature like this:
public List<Void> read( ... )
...什么?有没有理由这样做?这个 List
甚至可以保持什么?据我所知,实例化 Void
对象是不可能的。
... What? Is there ever a reason to do this? What could this List
even hold? As far as I was aware, it's not possible to instantiate a Void
object.
推荐答案
可能这个方法签名是作为某个泛型类的副产品而创建的。
It is possible that this method signature was created as a by-product of some generic class.
例如, SwingWorker
For example, SwingWorker
has two type parameters, one for final result and one for intermediate results. If you just don't want to use any intermediate results, you pass Void
as the type parameter, resulting in some methods returning Void
- i.e. nothing.
如果有一个方法 List< V>
,它会创建一个方法,就像您在问题中发布的一样。 SwingWorker
与 Void
作为类型参数中的returnAllIntermediateResults()
V
If there were a method List<V> returnAllIntermediateResults()
in SwingWorker
with Void
as the type parameter V
, it would have created a method just like you posted in your question.
代码完全有效。你可以使用类型参数 Void <>实例化
List
接口的任何实现(例如 ArrayList
/ code>。但是 Void
类型的唯一值可以是 null
。因此,如果实现允许 null
元素,那么该列表不能包含任何内容,但 null
s。
The code would be perfectly valid. You can instantiate any implementation of the List
interface (e.g. ArrayList
) with type parameter Void
. But the only value a Void
type can have is null
. So the list could not hold anything else but null
s, if the implementation allows null
elements.
这篇关于List< Void>的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!