为什么类名和构造函数名要相同 [英] Why should Class names and Constructors names be the same

查看:181
本文介绍了为什么类名和构造函数名要相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我最近开始学习面向对象理论的基础知识,并已通过Java进行了实践.我的问题很简单,为什么类名和构造函数名要相同?"

现在我已经阅读了这个问题->

为什么构造函数将始终与类具有相同的名称以及如何调用它们

尽管此响应是合理的,但减少了对关键字的使用,但在我看来,能够以不同于类的名称独立地命名构造函数更有意义.现在,如果我没记错(某种程度上已经过Google的快速验证),则语法如下:

  className objectName =新的builderName(变量) 

所以我的意思是,您已经命名了要从中创建对象的类,对于构造函数再次调用相同的名称有什么意义呢?您最好只写

  className objectName =新建(变量); 

看到两者给出的信息完全相同.

在我看来,能够独立命名构造函数以更好地了解其确切功能(例如,

  thisClass thisObject = New thisConstructorMakesThis(vars) 

除了上面链接中给出的响应之外,是否还有其他文档可以确切解释为什么构造函数和类被命名为相同的名字?

我还注意到响应中甚至没有明确提到动态命名的构造函数的考虑,而是显式语法" ,听起来更像是绝对的构造函数名称

解决方案

至少在C ++中,构造函数没有名称.需要某种语法来声明它们,因此决定使用类似函数声明语法的类名代替函数名.选择该名称而不是使用其他名称或完全不同的语法,没有特殊的原因.

在任何其他上下文中,类名都引用该类,而不是构造函数.

Java和C#都从C ++获取了许多语法,因此它们使用相同的约定.其他语言使用其他约定,例如Python中的 __ init __ .

您最好只写"className objectName = New(vars)",因为两者给出的信息完全相同.

除了变量并不总是对您正在创建的同一个类的引用;它通常是对基类的引用:

  base * something =新派生的; 

因此,新表达式确实需要指定要创建的类型,而不是要引用的类型.

此外,这些天您通常可以避免重复类名:

  auto p = std :: make_shared< thing>(vars); 

当然,C ++不会强制您进行动态分配:

 事物t(vars); 

在我看来,能够独立命名构造函数以更好地了解其确切作用会更加语义化

如果您需要比声明或new-expression语法所允许的表现力更高,则始终可以编写一个名为"factory"的函数(例如 make_shared )来创建和返回一个对象./p>

So I've recently started to learn the basics of Object Oriented Theory and have been practicing this via java. My question is simple, 'Why should Class names and Constructors names be the same'?

Now I've read through this question->

Why Constructor will always have same name as of class and how they are invoked

and even though this response makes sense, lower the use of keywords, it seems to me to make more sense to be able to name constructors differently and independently from the class. Now if I remember correctly (somewhat validated with a quick Google) the syntax goes like this:

 className objectName = New constructorName(variables)

So my point is you've already named the class you want to make an object from, What's the point of calling the same name again for the constructor? You might as well just write

 className objectName = New (vars); 

seeing as the two give exactly the same information.

It seems to me that it would be far more semantic to be able to independently name your constructor in order to get a better idea what exactly it does e.g.

  thisClass thisObject = New thisConstructorMakesThis(vars)

Is there some documentation other than the response given in the link above that explains exactly why constructors and classes are named the same?

Also I notice that the response doesn't even explicitly mention the consideration of dynamically named constructors, rather an "explicit syntax" which sounds more like an absolute constructor name.

解决方案

In C++ at least, constructors do not have names. Some kind of syntax is needed to declare them, and it was decided to use something like function declaration syntax with the class name in place of a function name. There's no particular reason why that was chosen rather than some other name or some completely different syntax.

In any other context, the class name refers to the class, not the constructor.

Java and C# both acquired much of their syntax from C++, so they use the same convention. Other languages use other conventions, such as __init__ in Python.

You might as well just write 'className objectName = New (vars)', seeing as the two give exactly the same information.

Except that the variable is not always a reference to the same class that you're creating; it's often a reference to a base class:

base * thing = new derived;

So the new-expression does need to specify the type you want to create, separately from the type you want to refer to it as.

Also, these days you can often avoid repeating the class name:

auto p = std::make_shared<thing>(vars);

and of course C++ doesn't force dynamic allocation on you:

thing t(vars);

It seems to me that it would be far more semantic to be able to independently name your constructor in order to get a better idea what exactly it does

You can always write a named "factory" function (like make_shared, for example) to create and return an object, if you need more expressiveness than the declaration or new-expression syntax allows.

这篇关于为什么类名和构造函数名要相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆