我的代码就像
public class Student : Person
{
public Student() : base()
{
m_name = "No Name";
}
public Student(string path) : base(path)
{
}
}
public class Person
{
public Person()
{
}
public Person(string path)
{
//..do something with path
}
}
现在,我想打电话给 -
Person myStudent = new Student("some path");
我希望它调用空的Ctor并调用base(path)ctor
所以我将获得一个学生实例,其属性为m_name =" No Name"
由于
答案 0 :(得分:1)
您可以使用可选参数来统一构造函数逻辑。
public class Student : Person
{
public Student(string path = null) : base(path)
{
}
}
public class Person
{
public Person(string path = null)
{
path = path ?? "sensible default";
}
}