在尝试调用基础构造函数时,它会抛出错误,例如“对象不包含带有一个参数的构造函数”
public string FirstName { get;private set; }
public string LastName { get;private set; }
public Employee(string firstName)
{
FirstName = firstName;
}
public Employee(string firstName,string lastName):base(firstName)//error
{
LastName = lastName;
}
public string SayHello()
{
return FirstName + " " + LastName;
}
感谢
答案 0 :(得分:8)
您可能想要调用 current clas 的构造函数,而不是基类构造函数:
public Employee(string firstName, string lastName): this(firstName) // this, not base
{
LastName = lastName;
}