使用构造函数链完成此构造的正确方法?
我的第一门 OOP 课程有一个作业,我理解所有内容,包括以下陈述:
您应该创建一个名为 ComplexNumber 的类。此类将包含定义为双精度数的私有数据成员中的复数的实部和虚部。您的类应该包含一个构造函数,允许将虚数的数据成员指定为构造函数的参数。默认(非参数化)构造函数应将数据成员初始化为 0.0。
当然,我知道如何创建这些构造函数而不将它们链接在一起,并且赋值不需要链接它们,但我想按照我喜欢的那样。
如果没有将它们链接在一起,我的构造函数看起来像这样:
class ComplexNumber
{
private double realPart;
private double complexPart;
public ComplexNumber()
{
realPart = 0.0;
complexPart = 0.0
}
public ComplexNumber(double r, double c)
{
realPart = r;
complexPart = c;
}
// the rest of my class code...
}
I have an assignment for my first OOP class, and I understand all of it including the following statement:
You should create a class called ComplexNumber. This class will contain the real and imaginary parts of the complex number in private data members defined as doubles. Your class should contain a constructor that allows the data members of the imaginary number to be specified as parameters of the constructor. A default (non-parameterized) constructor should initialize the data members to 0.0.
Of course I know how to create these constructors without chaining them together, and the assignment does not require chaining them, but I want to as I just like to.
Without chaining them together, my constructors look like this:
class ComplexNumber
{
private double realPart;
private double complexPart;
public ComplexNumber()
{
realPart = 0.0;
complexPart = 0.0
}
public ComplexNumber(double r, double c)
{
realPart = r;
complexPart = c;
}
// the rest of my class code...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是您要找的吗?
Is this what you're looking for?
@Rex 有链接的连接答案。
然而,在这种情况下,链接或任何初始化都是不必要的。 CLR 将在对象构造函数期间将字段初始化为其默认值。对于双精度数,这将导致它们被初始化为 0.0。因此,默认构造函数中的赋值并不是绝对必要的。
不过,有些人更喜欢显式初始化他们的字段以实现文档或可读性。
@Rex has the connect answer for chaining.
However in this case chaining or any initialization is not necessary. The CLR will initialize fields to their default value during object constructor. For doubles this will cause them to be initialized to 0.0. So the assignment in the default constructor case is not strictly necessary.
Some people prefer to explicitly initialize their fields for documentation or readability though.
通过在构造函数定义中使用“this”关键字进行构造函数链接的“方式”,如 Rex M 的示例所示。
构造函数链的“原因”是重用构造函数的实现。如果第二个构造函数的实现(主体)又长又复杂,那么您需要重用它(即链接到它或调用它),而不是将其复制粘贴到其他构造函数中。另一种方法可能是将在多个构造函数之间共享的代码放入一个公共子例程中,该子例程由多个构造函数调用:但是,不允许该子例程初始化
readonly
字段(这可以只能从构造函数初始化,而不是从子例程初始化),因此构造函数链接是一种解决方法。The 'how' of constructor chaining by using the 'this' keyword in the constructor definition, and shown in Rex M's example.
The 'why' of constructor chaining is to reuse the implementation of a constructor. If the implementation (body) of the 2nd constructor were long and complicated, then you'd want to reuse it (i.e. chain to it or invoke it) instead of copy-and-pasting it into other constructors. An alternative might be to put that code, which is shared between several constructors, into a common subroutine which is invoked from several constructors: however, this subroutine wouldn't be allowed to initialize
readonly
fields (which can only be initialized from a constructor and not from a subroutine), so constructor chaining is a work-around for that.