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

想在子类的字段、属性里调用父类的非静态字段、属性是不可行的,因为字段、属性的初始化是在类的构造函数之前。
比如子类调用父类的字段,需要初始化父类,但是父类没有初始化的时候你调用父类的字段、属性就会出错。

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

enter description here

1472487850077.jpg

posted @ 2016-08-30 00:26  代码小兵的成长  阅读(436)  评论(0编辑  收藏  举报