简体   繁体   English

为什么我们不能拥有名称与类名不同的构造函数,只要它们是唯一的

[英]Why cannot we have constructor with name different from class names as long as they are unique

I understand that Java imposes a restriction that class name should be same as constructor name. 我知道Java强加了一个限制,即类名应该与构造函数名相同。 Why is this restriction imposed? 为什么要施加这种限制? Why cannot I have a constructor of the class with a different name than class name as long as no constructor with the same name exists for any other class? 为什么我不能使用与类名不同的类的构造函数,只要不存在任何其他类的具有相同名称的构造函数?

This is because Java cannot "see" other classes, it only cares about the one class when it's compiling it. 这是因为Java无法“看到”其他类,它只关心编译它时的一个类。

Also, if the constructor is named something other than the class name, it cannot be found and it doesn't make any sense anyway. 此外,如果构造函数被命名为类名以外的东西,则无法找到它,无论如何它都没有任何意义。 Imagine doing this: 想象一下这样做:

Person person = new Robot();

where Robot would be a constructor in Person . Robot将成为Person的构造函数。 It just doesn't make any sense to humans or the compiler! 它对人类或编译器没有任何意义!

I guess the convention of having the same name for the class and it's construcors was considered useful. 我想这个类和它的构造函数具有相同名称的约定被认为是有用的。 Alternatively the language would have to be extended with a keyword ( constructor ?), method name conflicts resolved etc... 或者,语言必须用关键字( constructor ?)扩展,方法名称冲突解决等...

Can you share a use case that benefits from arbitrary named construcor method? 你能分享一个受益于任意命名的construcor方法的用例吗?

Let's assume that we can have another name rather than the class name, You have to check whether it's already been used for any other class or not. 让我们假设我们可以有另一个名字而不是类名,你必须检查它是否已经被用于任何其他类。 To elaborate more consider the following example, 详细说明请考虑以下示例,

Person p1 = new Vehicle(); 

Now, In another class 现在,在另一个班级

MotorCycle m1 = new Vehicle(); // if you have used the same name then it will create a conflict for the compiler which implementation should be considered. 

Now, think about the actual constructor naming conventions that if you use the same as for Class name, it's easier and also makes sense. 现在,考虑一下实际的构造函数命名约定,如果你使用与类名相同的命令,它会更容易也更有意义。

Person p1 = new Person();
MotorCycle m1 = new MotorCycle();

This convention is for programming ease, constructor chaining and consistency in the language. 此约定用于编程简易,构造函数链接和语言的一致性。

For example, consider a scenario where you want to use Scanner class, now what if the JAVA developers named the constructor as xyz! 例如,考虑一个您想要使用Scanner类的场景,现在如果JAVA开发人员将构造函数命名为xyz该怎么办!

Then how will you get to know that you need to write : 那你怎么知道你需要写:

Scanner scObj = new xyz(System.in) ; 扫描仪scObj = new xyz(System.in);

which could've have been really weird, right! 这可能真的很奇怪,对! Or, rather you might have to reference a huge manual to check constructor name of each class so as to get object created, which is again meaningless if you could have a solution of the problem by just naming constructors same as that of the class. 或者,您可能必须引用一个巨大的手册来检查每个类的构造函数名称以便创建对象,如果您只需命名构造函数与类的构造函数相同就可以解决问题,那么这也是毫无意义的。

Secondly, the constructor is itself created by the compiler if you don't explicitly provide it, then what could be the best name for constructor could automatically be chosen by the compiler so it is clear to the programmer! 其次,构造函数本身是由编译器创建的,如果你没有显式提供它,那么构造函数的最佳名称可以由编译器自动选择,所以程序员很清楚! Obviously, the best choice is to keep it the same as that of the class. 显然,最好的选择是保持与班级相同。

Thirdly, you may have heard of constructor chaining, then while chaining the calls among the constructors, how the compiler will know what name you have given to the constructor of the chained class! 第三,您可能听说过构造函数链接,然后在构造函数中链接调用时,编译器将如何知道您为链接类的构造函数指定了什么名称! Obviously, the solution to the problem is again same, KEEP NAME OF THE CONSTRUCTOR SAME AS THAT OF THE CLASS. 显然,问题的解决方案也是一样的,保持构造者的名称与类的一样。

Thanks for Asking. 谢谢你的要求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称与类名不同的构造方法 - Constructor with name different to the class name Jackson XML:如何从列表名称中获得不同的元素名称? - Jackson XML: How to have different element names from list name? Outputstream是一个抽象类,所以我们无法实例化它。为什么为Outputstream类提供默认构造函数? - Outputstream is an abstract class so we cannot instantiate it.Why then a default constructor is provided for Outputstream class? LinkedList数据结构,为什么我们需要从不同的构造函数中调用空的默认构造函数 - LinkedList data structure, why we need to call empty default constructor from a different constructor 既然抽象的 class 可以有一个公共的构造函数,为什么我们不能实例化它呢? - Since an abstract class can have a public constructor, why can't we instantiate it? java构造函数访问(如果子类中有私有构造函数) - java constructor accessing (if we have private constructor in child class) 无法从扩展DialogFragment的类创建默认构造函数,并且我已创建自己的自定义构造函数 - Cannot create a default constructor from a class that extends DialogFragment and I have created my own custom constructor 为什么我们不能从内部的类构造函数访问另一个内部类? - Why can't we get access from the inner's class constructor to another inner class? 我们是否必须从Object的子类中的Object调用默认构造函数 - Do we have to invoke default constructor from class Object in its' subclass 将参数从构造函数传递给不同类的构造函数 - Pass argument from a constructor to a constructor of a different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM