Java 泛型 void/Void 类型
我正在为 apache HttpClient 包实现一个 ResponseHandler
,如下所示:
new ResponseHandler<int>() {
public int handleResponse(...) {
// ... code ...
return 0;
}
}
但我希望 handleResponse
函数不返回任何内容,即 void
。这可能吗?以下代码无法编译,因为 void
不是有效的 Java 类型:
new ResponseHandler<void>() {
public void handleResponse(...) {
// ... code ...
}
}
我想我可以用 Void
替换 void
以返回 Void
对象,但这并不是我真正想要的。 问题:是否可以通过从 handleResponse
返回 void
的方式来组织此回调情况?
I am implementing a ResponseHandler
for the apache HttpClient package, like so:
new ResponseHandler<int>() {
public int handleResponse(...) {
// ... code ...
return 0;
}
}
but I'd like for the handleResponse
function to return nothing, i.e. void
. Is this possible? The following does not compile, since void
is not a valid Java type:
new ResponseHandler<void>() {
public void handleResponse(...) {
// ... code ...
}
}
I suppose I could replace void
with Void
to return a Void
object, but that's not really what I want. Question: is it possible to organize this callback situation in such a way that I can return void
from handleResponse
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Void
类型是针对这种具体情况而创建的:创建一个具有通用返回类型的方法,其中子类型可以是“void”。Void
的设计方式是不可能创建该类型的对象。因此,Void
类型的方法将始终返回null
(或异常完成),这几乎是您将得到的结果。您确实必须在方法中添加return null
,但这应该只是一个小小的不便。简而言之:一定要使用
Void
。The
Void
type was created for this exact situation: to create a method with a generic return type where a subtype can be "void".Void
was designed in such a way that no objects of that type can possibly be created. Thus a method of typeVoid
will always returnnull
(or complete abnormally), which is as close to nothing as you are going to get. You do have to putreturn null
in the method, but this should only be a minor inconvenience.In short: Do use
Void
.泛型仅处理对象类。泛型不支持 void 和原始类型,您不能将它们用作参数化类型。你必须使用 Void 来代替。
你能说说为什么你不想使用
Void
吗?Generics only handles object classes. void and primitive types are not supported by Generics and you cannot use these as a parameterized type. You have to use Void instead.
Can you say why you don't want to use
Void
?当需要返回
java.lang.Void
时,只需返回null
即可。When you need to return
java.lang.Void
, just returnnull
.泛型中不能有基元,因此
int
实际上是一个Integer
。对象Void
类似于泛型的关键字void
。You can't have primitives in generics so that
int
is actually anInteger
. The objectVoid
is analogous with the keywordvoid
for generics.Java 中的 java.lang.Void 实现是不言而喻的。另外,我写了一篇文章将其与泛型联系起来。在我开始理解这一点之前,我花了一些心思: http://www.siteconsortium.com/h /D00006.php。注意 TYPE = Class.getPrimitiveClass("void");
包 java.lang;
This java.lang.Void implementation in Java kind of speaks for itself. Also, I wrote an article that ties this into generics. It took a bit of thought before I started understanding this: http://www.siteconsortium.com/h/D00006.php. Notice TYPE = Class.getPrimitiveClass("void");
package java.lang;
唉,这是不可能的。您可以将代码设置为返回
Void
正如您所说,但是您永远无法实例化Void
所以您不能实际上编写一个符合此规范的函数。可以将其视为:泛型函数表示“此函数返回 X 类型的内容”,您可以指定 X,但不能将句子更改为“此函数不返回任何内容”。 (我不确定我是否同意这些语义,但它们就是这样。)
在这种情况下,我总是做的就是使函数返回类型
Object
,事实上总是返回null
。Alas it's not possible. You can set the code to return
Void
as you say, however you can never instanciate aVoid
so you can't actually write a function which conforms to this specification.Think of it as: The generic function says "this function returns something, of type X", and you can specify X but you can't change the sentence to "this function returns nothing". (I'm not sure if I agree with these semantics, but that's the way they are.)
In this case, what I always do, is just make the function return type
Object
, and in fact always returnnull
.