Java 泛型 void/Void 类型

发布于 2024-10-30 13:39:28 字数 668 浏览 3 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

痴骨ら 2024-11-06 13:39:28

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 type Void will always return null (or complete abnormally), which is as close to nothing as you are going to get. You do have to put return null in the method, but this should only be a minor inconvenience.

In short: Do use Void.

流心雨 2024-11-06 13:39:28

泛型仅处理对象类。泛型不支持 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?

£烟消云散 2024-11-06 13:39:28

当需要返回java.lang.Void时,只需返回null即可。

When you need to return java.lang.Void, just return null.

哭了丶谁疼 2024-11-06 13:39:28

泛型中不能有基元,因此 int 实际上是一个 Integer。对象 Void 类似于泛型的关键字 void

You can't have primitives in generics so that int is actually an Integer. The object Void is analogous with the keyword void for generics.

も星光 2024-11-06 13:39:28

Java 中的 java.lang.Void 实现是不言而喻的。另外,我写了一篇文章将其与泛型联系起来。在我开始理解这一点之前,我花了一些心思: http://www.siteconsortium.com/h /D00006.php。注意 TYPE = Class.getPrimitiveClass("void");

包 java.lang;

public final class Void {

    public static final Class<Void> TYPE = Class.getPrimitiveClass("void");

    private Void() {}
}

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;

public final class Void {

    public static final Class<Void> TYPE = Class.getPrimitiveClass("void");

    private Void() {}
}
箹锭⒈辈孓 2024-11-06 13:39:28

唉,这是不可能的。您可以将代码设置为返回 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 a Void 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 return null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文