表格<无效>在 Wicket 中(或一般使用 Void 类型)无效>
最近在 Wicket 中遇到这样的代码:
Form<?> form = new Form<Void>("form")
任何人都可以解释一下 Void 类型的用法吗?我第一次看到实际使用这种类型。它在 Wicket 之外都使用吗?
Came across code recently in Wicket like so:
Form<?> form = new Form<Void>("form")
Can anyone explain the usage of the Void type here? First time I've seen that type being used actually. Is it used t all outside of Wicket?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它在 Wicket 之外使用。例如,当方法采用
Callable
作为参数,并且我的 Callable 不返回任何内容时,我使用Callable
。Void
因此用于指示某些参数化方法不返回任何内容。在这种特殊情况下,根据文档,它用于指示表单没有任何模型对象。 Void 类型的唯一有效值为
null
。Yes, it's used outside of Wicket. For example, when a method takes a
Callable<V>
as argument, and my Callable doesn't return anything, I use aCallable<Void>
.Void
is thus used to indicate that some parameterized method doesn't return anything.In this particular case, according to the documentation, it's used to indicate that the Form doesn't have any model object. The only valid value of the Void type is
null
.当您想在类型中表达“不想返回任何内容”或“不想传递任何内容”时,有时会使用
Void
作为类型参数。即不包含任何信息的单元类型。因为类型参数必须是引用类型,并且null
是所有引用类型的值,所以所需的类型不会有任何其他值,即没有实例的类型。 “空类型”本身可以工作,但它没有名称。因此,我们选择一个没有任何实例的任意类,而Void
是一个方便的选择。从技术上讲,任何其他不可实例化的类,例如实用程序类Math
或Collections
,都可以正常工作;但Void
似乎更合适,因为它与void
相关,后者用于在基本类型中表达单元类型。Void
is sometimes used as the type parameter in cases when you want to express that you "don't want to return anything" or "don't want to pass anything" in the type. i.e. a unit type that holds no information. Because a type parameter has to be a reference type, andnull
is a value of all reference types, the desired type would not have any other values, i.e. is a type with no instances. The "null type" itself would work, but it doesn't have name. So we pick an arbitrary class that doesn't have any instances, andVoid
is a convenient choice. Technically any other non-instantiatable class, like the utility classesMath
orCollections
, would work just as well; butVoid
seems more appropriate because of its relation tovoid
, the type that is used to express a unit type within primitive types.