本文介绍了返回一个Void对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当它不是一个基元时,返回 Void
类型的正确方法是什么?例如。
interface B< E> {E method(); }
$ b $ class A implements B< Void> {
public Void method(){
// do something
return null;
$ div $解析方案
因此,以下任何一项都足够了:
- 使用
Object
参数化并返回 new Object()或 null
- 参数化为
Void
并返回 null
- 用您的
NullObject
参数化
您不能使这个方法 void
,而其他任何东西都返回 。由于某些内容被忽略,您可以返回任何内容。
What is the correct way to return a Void
type, when it isn't a primitive? Eg. I currently use null as below.
interface B<E>{ E method(); }
class A implements B<Void>{
public Void method(){
// do something
return null;
}
}
解决方案
So any of the following would suffice:
- parameterizing with
Object
and returning new Object()
or null
- parameterizing with
Void
and returning null
- parameterizing with a
NullObject
of yours
You can't make this method void
, and anything else returns something. Since that something is ignored, you can return anything.
这篇关于返回一个Void对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!