有人可以解释 Java 中的 void 返回类型吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18689883/
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
Can someone explain a void return type in Java?
提问by Ethan
The only void return type I have seen has System.out.println statements in a method.
So once the method is called those Strings will be printed.
Couldn't you make the return type string and have the string returned instead of doing void return type?
我见过的唯一 void 返回类型在方法中包含 System.out.println 语句。因此,一旦调用该方法,将打印这些字符串。
您不能创建返回类型字符串并返回字符串而不是执行 void 返回类型吗?
If the void return type method has other methods in it could you make the return type the value which the method gives which would return the outcome of that method?
如果 void 返回类型方法中有其他方法,您能否使返回类型成为该方法给出的值,该值将返回该方法的结果?
When is it that you could only use the void return type?
什么时候只能使用 void 返回类型?
采纳答案by Stephen C
Can someone explain a void return type in Java?
有人可以解释 Java 中的 void 返回类型吗?
The void
type is used to declare that a method does not return a value.
该void
类型用于声明方法不返回值。
Couldn't you just make the return type String and set the string equal to the parameter to make it show up when the method is called?
难道您不能只使返回类型为 String 并将字符串设置为等于参数以使其在调用方法时显示出来吗?
Hypothetically "you" (or in this case, the designers of the PrintStream
API) could do that, but there is no point in doing it. I am struggling think of a plausible use-case where it would make sense to usethe println
argument String ... if it was returned as a result.
假设“你”(或者在这种情况下,PrintStream
API的设计者)可以做到这一点,但这样做没有意义。我奋力想一个合理的用例,其中它将使意义的使用的println
参数字符串...如果它被作为结果返回。
Bear in mind the primary goals of a good API design include1:
请记住,良好的 API 设计的主要目标包括1:
- to support the common use-cases well, and
- to be easy for programmers to understand.
- 很好地支持常见用例,以及
- 便于程序员理解。
Methods that return values that either don't make sense or that are rarely (if ever) used are (IMO) poorly designed.
返回没有意义或很少(如果曾经)使用的值的方法(IMO)设计得很差。
If the void return type method has other methods in it could you make the return type method which would return the outcome of that method?
如果 void 返回类型方法中有其他方法,您能否制作返回该方法结果的返回类型方法?
Well, I guess so. But you've got the same issues as above. If the result is either rarely used or is hard to understand (and therefore hard to use correctly) then it is probably bad designto return it.
嗯,我想是的。但是您遇到了与上述相同的问题。如果结果要么很少使用,要么很难理解(因此很难正确使用),那么返回它可能是糟糕的设计。
When is it that you could only use the void return type?
什么时候只能使用 void 返回类型?
One case is where you are implementing or overriding a method in an interface
or a superclass, and that method is declared with a void
return type.
一种情况是您正在实现或覆盖interface
超类中的方法,并且该方法使用void
返回类型声明。
But in isolation, there are no cases where you canonly use void
. (But there are lots of caseswhere good design says that it is best to use void
!)
但孤立地看,不存在情况下,你可以只使用void
。(但在很多情况下,好的设计说最好使用void
!)
1 - There other goals too. Please don't take this out of context ...
1 - 还有其他目标。请不要断章取意...
回答by Chris
Simply put, void returns nothing and expects nothing to be returned.
简而言之,void 不返回任何内容并且不期望返回任何内容。
you can read more about return here:
您可以在此处阅读有关退货的更多信息:
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
回答by Smitty
A void return type simply means nothing is returned. System.out.println does not return anything as it simply prints out the string passed to it as a parameter.
void 返回类型仅表示不返回任何内容。System.out.println 不返回任何内容,因为它只是打印出作为参数传递给它的字符串。
And..
和..
Couldn't you just make the return type String and set the string equal to the parameter to make it show up when the method is called?
难道您不能只使返回类型为 String 并将字符串设置为等于参数以使其在调用方法时显示出来吗?
I have no idea what you are trying to say with this.
我不知道你想用这个说什么。
回答by andy256
The void
return type is used to say that the method doesn't return anything.
在void
返回类型常说,该方法不返回任何东西。
It can be a subject of debate whether methods should return void
, one of the arguments (we might do that if there was only one), or this
(we might do that if we want to support chaining of methods calls).
方法是否应该返回void
,参数之一(如果只有一个,this
我们可能会这样做),或者(如果我们想要支持方法调用的链接,我们可能会这样做)可能是一个争论的主题。
So this decision is part of class design.
所以这个决定是类设计的一部分。
回答by user3580271
Void is used when your method doesn't returnanything.
当您的方法不返回任何内容时,将使用 Void 。
Print statement is for printing not for returning.
If you want a String to be returned from your method. remove void and put String keyword there. And as the last line of the method type the return statement. return s;
s is your String variable.
打印语句是为了打印而不是为了返回。如果您希望从您的方法返回一个字符串。删除 void 并将 String 关键字放在那里。并在方法的最后一行键入 return 语句。return s;
s 是您的 String 变量。
If you write a method without return statement . you have to write that method as void
. void means you are not returning anything. returning anything is closed.
如果你写了一个没有 return 语句的方法。您必须将该方法编写为void
. void 意味着你没有返回任何东西。返回任何东西都是关闭的。