定义一个父类,有姓氏字段,然后定义孩子类,那么如何调用父类构造函数给子类字段赋值呢作者: 史金阳 时间: 2013-3-7 13:57
个人理解 是想这样吗?
public class Father
{
//成员变量
protected string firstName;
protected int wealth;
protected char blood;
//构造函数
public Father(string _firstName, int _wealth, char _blood)
{
this.firstName = _firstName;
this.wealth = _wealth;
this.blood = _blood;
}
}
//子类 继承
public class Son : Father
{
//子类成员变量
public string PlayGame;
//调用父类构造函数
public Son(string _firstName, int _wealth, char _blood, string _PlayGame) :
base(_firstName,_wealth,_blood)
{
this.PlayGame = _PlayGame;
}
public void aa() //方法
{
Console.WriteLine("我姓{0},财产有{1}元,血型是{2},我会玩{3}",this.firstName,this.wealth,this.blood,this.PlayGame);
}
}
class Program
{
static void Main(string[] args)
{
Son son = new Son("史",0,'a',"连连看");//创建子类对象
son.aa();//调用方法
Console.ReadKey();
}
}作者: 余攀 时间: 2013-3-8 21:34
史金阳 发表于 2013-3-7 13:57
个人理解 是想这样吗?
public class Father
{