【发布时间】:2016-09-19 20:24:33
【问题描述】:
如何在c#中引用另一个构造函数?例如
class A
{
A(int x, int y) {}
A(int[] point)
{
how to call A(point.x, point.y}?
)
}
【问题讨论】:
-
可以在构造函数中使用
keywordthis
标签: c# constructor
如何在c#中引用另一个构造函数?例如
class A
{
A(int x, int y) {}
A(int[] point)
{
how to call A(point.x, point.y}?
)
}
【问题讨论】:
keyword this
标签: c# constructor
这很简单。与调用基本构造函数的方式相同。
A(int[] point) : this(point[0], point[1])
{
}
【讨论】:
base 构造函数。
您可以在“派生”构造函数中使用关键字this 来调用“this”构造函数:
class A
{
A(int x, int y) {}
A(int[] point) : this(point[0], point[1]) { //using this to refer to its own class constructor
{
}
}
除此之外,我认为您应该通过索引获取数组中的值:point[0], point[1],而不是像获取字段/属性那样做:point.x, point.y
【讨论】:
this 构造函数,而不是 base 构造函数。