Java 如果方法的返回类型是 Void,我应该返回什么?(不作废!)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do I return if the return type of a method is Void? (Not void!)
提问by Daniel Rikowski
Due to the use of Generics in Java I ended up in having to implement a function having Void
as return type:
由于在 Java 中使用泛型,我最终不得不实现一个具有Void
返回类型的函数:
public Void doSomething() {
//...
}
and the compiler demands that I return something. For now I'm just returning null
, but I'm wondering if that is good coding practice...
并且编译器要求我返回一些东西。现在我只是回来了null
,但我想知道这是否是好的编码习惯......
I'm asking about V?oid, not v?oid. The class Void
, notthe reserved keyword void
.
我问的是V?oid,而不是V?oid。类Void
,而不是保留关键字void
。
I've also tried Void.class
, void
, Void.TYPE
, new Void()
, no return at all, but all that doesn't work at all. (For more or less obvious reasons) (See this answerfor details)
我也试过Void.class
, void
, Void.TYPE
, new Void()
, 根本没有回报,但所有这些都不起作用。(出于或多或少明显的原因)(有关详细信息,请参阅此答案)
- So what am I supposed to return if the return type of a function is
Void
? - What's the general use of the
Void
class?
- 那么如果函数的返回类型是 ,我应该返回
Void
什么? Void
类的一般用途是什么?
采纳答案by John Feminella
So what am I supposed to return if the return type of a function has to be
Void
?
那么如果函数的返回类型必须是,我应该返回
Void
什么?
Use return null
. Void
can't be instantiated and is merely a placeholder for the Class<T>
type of void
.
使用return null
. Void
不能被实例化并且仅仅是为一个占位符Class<T>
的类型void
。
What's the point of
Void
?
有什么意义
Void
?
As noted above, it's a placeholder. Void
is what you'll get back if you, for example, use reflection to look at a method with a return type of void
. (Technically, you'll get back Class<Void>
.) It has other assorted uses along these lines, like if you want to parameterize a Callable<T>
.
如上所述,它是一个占位符。Void
例如,如果您使用反射查看返回类型为void
. (从技术上讲,你会回来的Class<Void>
。)沿着这些路线它还有其他各种用途,比如如果你想参数化一个Callable<T>
.
Due to the use of generics in Java I ended up in having to implement this function
由于在 Java 中使用泛型,我最终不得不实现这个功能
I'd say that something may be funky with your API if you needed to implement a method with this signature. Consider carefully whether there's a better way to do what you want (perhaps you can provide more details in a different, follow-up question?). I'm a little suspicious, since this only came up "due to the use of generics".
我会说,如果您需要使用此签名来实现一个方法,那么您的 API 可能会很时髦。仔细考虑是否有更好的方法来做你想做的事(也许你可以在不同的后续问题中提供更多细节?)。我有点怀疑,因为这只是“由于使用泛型”而出现的。
回答by Bombe
return null
is the way to go.
return null
是要走的路。
回答by PhiLho
If, for obscure reasons, you MUST use this type, then indeed returning null seems to be a sensible option, since I suppose return value will not be used anyway.
The compiler will force you to return something anyway.
And this class doesn't seem to have a public constructor so new Void() is not possible.
如果出于晦涩的原因,您必须使用这种类型,那么确实返回 null 似乎是一个明智的选择,因为我认为无论如何都不会使用返回值。
编译器无论如何都会强迫你返回一些东西。
而且这个类似乎没有公共构造函数,所以 new Void() 是不可能的。
回答by Jon Bright
There's no way to instantiate a Void, so the only thing you canreturn is null.
没有办法实例化一个 Void,所以你唯一可以返回的是 null。
回答by Martijn
To make clear why the other suggestions you gave don't work:
要弄清楚为什么您提供的其他建议不起作用:
Void.class
and Void.TYPE
point to the same object and are of type Class<Void>
, not of Void
.
Void.class
并Void.TYPE
指向同一个对象,并且属于 类型Class<Void>
,而不是Void
。
That is why you can't return those values. new Void()
would be of type Void
but that constructor doesn't exist. In fact, Void
has no public constructors and so cannot be instantiated: You can never have any object of type Void
except for the polymorphic null
.
这就是为什么你不能返回这些值。new Void()
将是类型,Void
但该构造函数不存在。事实上,Void
它没有公共构造函数,因此不能被实例化:Void
除了多态之外,你永远不能拥有任何类型的对象null
。
Hope this helps! :-)
希望这可以帮助!:-)
回答by Jang-Ho Bae
just like this.
像这样。
public Class TestClass {
public void testMethod () {
return;
}
}