码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-08-30 01:51:18      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

标签:

原文地址:http://www.cnblogs.com/zbqldyj/p/5820309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!