父类构造函数给子类赋值,子类的构造函数要用base
比如父类有Name,Age属性,父类的构造函数是
public Father(string name,int age)
{
this.Name = name;
this.Age = age;
}
子类Son多了个收入Income属性,子类的构造函数应该是
public Son(string name,int age,int income)
:base(name,age)
{
this.Income = income;
}
这样就可以调用父类构造函数给子类赋值,
实例化子类的时候是,
Father son = new Son("王五",25,3000); |