【发布时间】:2011-08-04 16:03:38
【问题描述】:
我目前正在学习 C#,并且正在学习构造函数和构造函数的链式调用,以便不必在每个构造函数中粘贴相同的代码(变量的相同值)。
我有三个构造函数,一个没有参数,一个有三个参数,一个有四个参数。我要做的是,使用默认构造函数调用具有三个参数的构造函数,传递参数(变量)和具有三个参数的构造函数的默认值,我正在寻找调用具有四个参数的构造函数参数。我似乎有第一个排序列出默认值,但我正在努力如何编写具有三个参数的构造函数,然后在需要时让它调用具有四个参数的构造函数。
默认构造函数应该将所有类型为 string 的实例变量赋值给 string.Empty。
public Address()
{
m_street = string.Empty;
m_city = string.Empty;
m_zipCode = string.Empty;
m_strErrMessage = string.Empty;
m_country = Countries;
}
public Address(string street, string city, string zip)
{
}
public Address(string street, string city, string zip, Countries country)
{
}
我希望执行以下操作,但它不起作用:-
public Address(string street, string city, string zip)
: this street, string.Empty, city, string.Empty, zip, string.Empty
{
}
【问题讨论】:
-
看起来你忘记了一些括号。还有几个关键词……
标签: c# parameters constructor chaining