Can someone explain a void return type in Java?
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?
假设“你”(或者在这种情况下,PrintStream API的设计者)可以做到这一点,但这样做是没有意义的.我正在努力想到一个看似合理的用例,使用println参数String是有意义的…如果它作为结果返回.
请记住,良好API设计1的主要目标是:
>以便很好地支持常见用例
>让程序员容易理解.
返回值没有意义或很少(如果曾经)使用过的值的方法是(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?
嗯,我想是的.但是你有与上面相同的问题.如果结果很少使用或难以理解(因此难以正确使用)那么返回它可能是糟糕的设计.
When is it that you could only use the void return type?
一种情况是您在接口或超类中实现或重写方法,并且该方法使用void返回类型声明.
但是在孤立的情况下,没有任何情况下你只能使用void. (但是很多情况下,优秀的设计表明最好使用void!)
1 – 还有其他目标.请不要脱离这种情况……