如何链接这些构造函数(C#)?

发布于 2024-08-12 14:32:26 字数 573 浏览 2 评论 0原文

我刚刚了解了链接构造函数的概念,但我不知道如何将这两个特定的构造函数链接在一起,所以如果有人可以帮助我,我将不胜感激。

谢谢!

构造函数

// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Line()
{
    startPoint.xCoord = 0;
    startPoint.yCoord = 0;
    endPoint.xCoord = 0;
    endPoint.yCoord = 0;
}


// parameterized constructor
// purpose: initialize data members to p1 and p2
// Parameters: Point objects p1 and p2
// returns: none
public Line(Point p1, Point p2)
{
    startPoint = p1;
    endPoint = p2;
}

I'm just getting the concept of chaining constructors down, but I can't figure out how to chain these two particular constructors together, so I would appreciate it if somebody could help me out.

Thanks!

Constructors

// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Line()
{
    startPoint.xCoord = 0;
    startPoint.yCoord = 0;
    endPoint.xCoord = 0;
    endPoint.yCoord = 0;
}


// parameterized constructor
// purpose: initialize data members to p1 and p2
// Parameters: Point objects p1 and p2
// returns: none
public Line(Point p1, Point p2)
{
    startPoint = p1;
    endPoint = p2;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

忘你却要生生世世 2024-08-19 14:32:26
public Line() : this(new Point(), new Point())
{
}
public Line() : this(new Point(), new Point())
{
}
勿忘初心 2024-08-19 14:32:26

将这些链接起来确实没有任何好处。

There's really no win in chaining these.

孤独岁月 2024-08-19 14:32:26

尝试以下操作

public Line() : this(new Point(0,0), new Point(0,0))
{
}

Try the following

public Line() : this(new Point(0,0), new Point(0,0))
{
}
无风消散 2024-08-19 14:32:26

这对你有用

// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Line() : this (new Point(0, 0), new Point(0, 0))
{

}

This will work for you

// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Line() : this (new Point(0, 0), new Point(0, 0))
{

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