java 与构造函数同名的方法 - 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3401444/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:40:31  来源:igfitidea点击:

Methods With Same Name as Constructor - Why?

javaoop

提问by Tom Tresansky

Why is the following allowed:

为什么允许以下内容:

public class Foo {
  public Foo() { ... }
  public void Foo() { ... }
}

Is there ever a valid reason for naming a method the same as the class?

将方法命名为与类相同的方法是否有正当理由?

采纳答案by bcat

My guess is that it's allowed because explicitly disallowing it would add another requirement to Java's identifier naming rules for very little benefit. Unlike, say, C++, Java always requires that constructors are called with the newkeyword, so there's never any ambiguity about whether an identifier refers to a method or a constructor. I do agree that a method with the same name as its parent class is quite confusing at first glance, and it should be almost certainly be avoided. That said, I'm glad they chose not to further complicate the language by banning such methods.

我的猜测是它是允许的,因为明确禁止它会向 Java 的标识符命名规则添加另一个要求,但好处很少。与 C++ 不同,Java 总是要求使用new关键字调用构造函数,因此对于标识符是指方法还是构造函数,从来没有任何歧义。我确实同意与其父类同名的方法乍一看非常令人困惑,几乎肯定应该避免使用。也就是说,我很高兴他们选择不通过禁止此类方法使语言进一步复杂化。

回答by duffymo

This is heinous. I would not allow such a thing to survive a code review.

这是令人发指的。我不会允许这样的事情在代码中幸存下来。

回答by emory

The only valid reason - that I can think of - for utilizing this is if the parent class adds methods after you have written the child class:

唯一有效的原因——我能想到的——使用它是如果在你编写了子类之后父类添加了方法:

interface Father { } // version 1.0

class Son implements Father {  Son ( ) { } } // version 1.0

interface Father { void Son ( ) ; /* new method */ } // version 2.0

class Son implements Father { Son ( ) { } void Son ( ) { } } // version 2.0

If you have no control of the Fatherinterface, but do have control of the Sonclass, then you have to change Sonsomehow. You could

如果您无法控制Father界面,但可以控制Son类,那么您必须以Son某种方式进行更改。你可以

  1. Change the name of the Sonclass
  2. Break the relationship with the Fatherclass
  3. Add the Sonmethod
  1. 更改Son班级名称
  2. 打破与Father班级的关系
  3. 添加Son方法

If the Sonclass is a dependency of many other projects then #1 and #2 may be unacceptable, leaving you with no choice but to add the Son( )method.

如果Son该类是许多其他项目的依赖项,那么 #1 和 #2 可能是不可接受的,您别无选择,只能添加该Son( )方法。

This is a highly contrived example. You should never see such a thing in real code.

这是一个非常人为的例子。你永远不应该在真正的代码中看到这样的事情。

回答by samitgaur

The constructor has the same name as the class and falls under the Type namespace. Method namespace is distinct from Type namespace in Java. So this is technically allowed (and this is not overloading).

构造函数与类同名,属于 Type 命名空间。方法命名空间与 Java 中的类型命名空间不同。所以这在技术上是允许的(这不是超载)。

However, there isn't ANY valid reason to actually name a method the same as the class name. It will be considered as a very bad practice.

但是,没有任何正当理由将方法实际命名为与类名相同的名称。这将被视为非常糟糕的做法。

回答by Adrian Russell

I cannot see a valid reason for it.

我看不出有什么正当理由。

C# provides a compiler error - "Member Names Cannot Be The Same As Their Enclosing Type"

C# 提供编译器错误 - “成员名称不能与其封闭类型相同”

It could be quite confusing, especially when using reflection to look at the composition of an object.

这可能会令人困惑,尤其是在使用反射查看对象的组成时。

回答by Jesse Jashinsky

The method's return value is defined as void, whereas the constructor has no return value, which makes them different and thus valid code. Why someone would want to do that, however, is beyond me.

方法的返回值被定义为void,而构造函数没有返回值,这使得它们不同,因此是有效的代码。然而,我无法理解为什么有人想要这样做。

回答by hny

the reason is that in constructors we pass the values at the time of the object creation of the class.so thats why the name of the constructor should be the same so that it can take passed values and initialized at the time of the object creation of the class.

原因是在构造函数中,我们在类的对象创建时传递值。这就是为什么构造函数的名称应该相同,以便它可以接受传递的值并在创建对象时初始化班上。