Java泛型void / Void类型 [英] Java generics void/Void types

查看:580
本文介绍了Java泛型void / Void类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为apache HttpClient包实现 ResponseHandler ,如下所示:

I am implementing a ResponseHandler for the apache HttpClient package, like so:

new ResponseHandler<int>() {
    public int handleResponse(...) {
        // ... code ...
        return 0;
    }
}

但我想要 handleResponse 函数不返回任何内容,即 void 。这可能吗?以下内容无法编译,因为 void 不是有效的Java类型:

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 ...
        }
}

我想我可以用 void > Void 返回 Void 对象,但这不是我想要的。 问题:是否可以组织此回调情况,以便从 handleResponse void c $ c>?

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?

推荐答案

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

你能说出为什么你不想使用 Void

Can you say why you don't want to use Void?

这篇关于Java泛型void / Void类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆