使用构造函数链完成此构造的正确方法?

发布于 2024-08-13 03:24:11 字数 638 浏览 3 评论 0原文

我的第一门 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-08-20 03:24:11

这是您要找的吗?

public ComplexNumber()
    : this(0.0, 0.0)
{
}

public ComplexNumber(double r, double c)
{
     realPart = r;
     complexPart = c;
}

Is this what you're looking for?

public ComplexNumber()
    : this(0.0, 0.0)
{
}

public ComplexNumber(double r, double c)
{
     realPart = r;
     complexPart = c;
}
深海夜未眠 2024-08-20 03:24:11

@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.

你是暖光i 2024-08-20 03:24:11

我仍在尝试掌握构造函数链接的概念,因此它可以工作,但为什么/如何?

通过在构造函数定义中使用“this”关键字进行构造函数链接的“方式”,如 Rex M 的示例所示。

构造函数链的“原因”是重用构造函数的实现。如果第二个构造函数的实现(主体)又长又复杂,那么您需要重用它(即链接到它或调用它),而不是将其复制粘贴到其他构造函数中。另一种方法可能是将在多个构造函数之间共享的代码放入一个公共子例程中,该子例程由多个构造函数调用:但是,不允许该子例程初始化 readonly 字段(这可以只能从构造函数初始化,而不是从子例程初始化),因此构造函数链接是一种解决方法。

I am still trying to grasp the concept of constructor-chaining, so it works, but why/how?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文