在 void 方法中返回 void? [英] return void in a void method?

查看:93
本文介绍了在 void 方法中返回 void?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以做到:

void someMethod(){
 return;
 }

但我在

void someMethod(){
return void;
}

为什么不允许后者?这对我来说更有意义.

Why is the latter not allowed? It makes more sense to me.

我知道什么是 void 方法,而且我根本不需要从它返回(并且可能不应该,在大多数情况下)但我不明白为什么我不能返回 void从空方法.在我看来,如果您能够编写 return; ,则方法声明中不应该有关键字(如构造函数).

I know what a void method is, and that I don't have to return from it at all(and probably shouldn't, in most cases) but I don't understand why I can't return void from a void method. In my opinion, there should be no keyword in the method declaration (like constructors) if the you are able to write return;.

推荐答案

我认为两者都应避免.我更喜欢这个:

I think both are to be shunned. I prefer this:

void someMethod() {
    // do stuff; no return at bottom
}

我愿意让您在 JDK 源代码中找到许多类似这样的方法.

I'd be willing to be that you'd find lots of methods in the JDK source code that look like this.

这篇关于在 void 方法中返回 void?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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