如何将基础构造函数数据用于同一个类中的另一个构造函数?

时间:2014-10-31 10:36:14

标签: c# console-application

在尝试调用基础构造函数时,它会抛出错误,例如“对象不包含带有一个参数的构造函数”

    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;
    }

感谢

1 个答案:

答案 0 :(得分:8)

您可能想要调用 current clas 的构造函数,而不是基类构造函数:

public Employee(string firstName, string lastName): this(firstName) // this, not base
{
    LastName = lastName;
}