为什么java允许具有类名和类型void的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18480867/
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
Why java allows method that has class name and type void
提问by Malintha
Java allows to create method which has the name of the class and type void ( Like void constructor). Constructor has no type and it do the function of the constructor. But is there any usage above mentioned kind of methods. Can you give examples of those usages
Java 允许创建具有类名和类型 void(如 void 构造函数)的方法。构造函数没有类型,它完成构造函数的功能。但是有没有上面提到的那种方法的用法。你能举例说明这些用法吗
Sample Code:
示例代码:
//my class
class MyClass{
//constructor
public MyClass(.....){
}
//What is the use of the below method
public void MyClass(....){
}
}
采纳答案by rocketboy
To answer your question: No, it has no special use. In fact, it is counter intuitive and confusing. Some compilers will even generate a warning "This method has a constructor name".
回答您的问题:不,它没有特殊用途。事实上,这是反直觉和混乱的。某些编译器甚至会生成警告“此方法具有构造函数名称”。
But because technically it is possible that it is nota compilation error, I would advice staying away from it. There are several different method names which can be more descriptive and serve the same purpose.
但是因为从技术上讲它可能不是编译错误,我建议远离它。有几种不同的方法名称可以更具描述性并用于相同的目的。
回答by Kon
The usage is identical to that of any other method. And the return type need not be void
. It can often be confusing, but it is perfectly legal to name methods the same as the class name. It'll usually cause more confusion then you want, but it's a legal behavior. The methods have no special properties apart from any other class method.
用法与任何其他方法相同。并且返回类型不必是void
. 这通常会令人困惑,但将方法命名为与类名相同是完全合法的。它通常会引起比您想要的更多的混乱,但这是一种合法行为。除了任何其他类方法之外,这些方法没有任何特殊属性。
回答by Malintha
Yes, A fresher to Java may confuse with this. The constructor cannot have a return type. But some people misunderstand that the "no return type" and "void" are some what equal but it is not. Constructor is a different story and the method that has the class name and any other return type (void, String, int, .......) is different. But it is more confusing.
是的,Java 新手可能会与此混淆。构造函数不能有返回类型。但是有些人误解了“无返回类型”和“void”是相等的,但事实并非如此。构造函数是另一回事,具有类名和任何其他返回类型(void、String、int、......)的方法是不同的。但它更令人困惑。
回答by Stephen C
There is no sensibleusage for a method those name is the same as the class name.
对于名称与类名相同的方法,没有合理的用法。
It is a style violation. According to the official Java style guide, names of Java methods should start with a lower case letter.
It is confusing because it looks superficially like a constructor.
It is confusing because when you use such a method it looks like you are using the classname incorrectly.
It is possible that this will result in unexpected behaviour and/or unexpected compilation errors due to the class-name vs method-name ambiguity.
这是一种风格违规。根据官方 Java 风格指南,Java 方法的名称应以小写字母开头。
它令人困惑,因为它表面上看起来像一个构造函数。
这是令人困惑的,因为当您使用这样的方法时,您似乎在错误地使用类名。
由于类名与方法名的歧义,这可能会导致意外行为和/或意外编译错误。
Why java allows method that has class name and type void?
为什么java允许具有类名和类型void的方法?
Basically because the Java language does not enforcethe identifier style rules. (IMO, it would have been better if it did enforce the rules ... but the decision was made a long time ago, and it can't be changed for compatibility reasons.)
基本上是因为 Java 语言不强制执行标识符样式规则。(IMO,如果它确实执行规则会更好......但这个决定是很久以前做出的,并且出于兼容性原因无法更改。)
回答by Jayaram
No It don't have special usage, it will be treated as similar to other methods inside the class.
否 没有特殊用法,会被视为与类内的其他方法类似。
It will be worth reading below article:
以下文章值得一读:
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
If the method name is same as class name and it has no return type then its known as constructor
which has special usage in oops.
如果方法名与类名相同并且没有返回类型,那么它constructor
在 oops 中被称为which 有特殊用法。
by keeping such names as method it will only create a confusion and code readabilty.
保留这样的名称作为方法只会造成混乱和代码可读性。
below link will might help you why readibility matters: http://java.dzone.com/articles/why-code-readability-matters
下面的链接可能会帮助你为什么可读性很重要:http://java.dzone.com/articles/why-code-readability-matters