gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:00:41 27 4
gpt4 key购买 nike

我的第一个 OOP 类(class)有一个作业,我理解所有作业,包括以下语句:

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.

我当然知道如何在不将它们链接在一起的情况下创建这些构造函数,并且赋值不需要将它们链接在一起,但我想这样做,因为我喜欢这样做。

如果不将它们链接在一起,我的构造函数如下所示:

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

最佳答案

这是您要找的吗?

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

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

关于c# - 使用构造函数链来完成此构造的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1837087/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com