您的位置:首页 > 其它

每日问题之子类字段属性使用父类的非静态字段属性错误

2016-08-30 00:26 337 查看
想在子类的字段、属性里调用父类的非静态字段、属性是不可行的,因为字段、属性的初始化是在类的构造函数之前。

比如子类调用父类的字段,需要初始化父类,但是父类没有初始化的时候你调用父类的字段、属性就会出错。

class Son: Father
{
public string sonKey = "son" + base.fatherKye;
public void sonMethod()
{
sonKey = "son" + base.fatherKye;
}
public Son()
{
}
static void Main(string[] args)
{
new Son().sonMethod();
}
}

class Father
{
public Father()
{
}

public  string fatherKye { get; set; }

public void fatherMethod()
{
Console.WriteLine(fatherKye);
}
}




1472487850077.jpg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: