我学习 C#,我试图理解重载的构造函数,它是如何工作的,以及像链调用一样使用它们的意义?为什么不只有一个具有所有必要参数的构造函数?下面我有一些我正在处理的任务的帮助代码,我需要一些帮助来理解所有这些构造函数的要点。一些帮助!谢谢!
public class Email
{
//private email
private string m_personal;
//work mail
private string m_work;
public Email()
{
}
public Email(string workMail) : this(workMail, string.Empty)
{
}
public Email(string workMail, string personalMail)
{
m_work = workMail;
m_personal = personalMail;
}
public string Personal
{
//private mail
get { return m_personal; }
set { m_personal = value; }
}
public string Work
{
get { return m_work; }
set { m_work = value; }
}
public string GetToStringItemsHeadings
{
get { return string.Format("{0,-20} {1, -20}", "Office Email", "Private Email"); }
}
public override string ToString()
{
string strOut = string.Format("{0,-20} {1, -20}", m_work, m_personal);
return strOut;
}
}