010 this关键字


this不光可以用在
引用类的字段,如:
this.name = name

也可以,引用构造函数,规避重复的初始化字段,如:

    class Customer
    {
        public int ID;
        public string Name;
        public string Ex;
        public Customer()
        {
            this.Ex = "附加字段";
        }
        public Customer(int id): this()  // 引用构造函数
        {
            this.ID = id;
        }
        public Customer(int id, string name): this(id) // 引用构造函数
        {
            this.Name = name;
        }
    }

 

如果类有继承的父类,base关键字,有类似的用法base()

 

posted @ 2020-09-08 09:50  Transcog  阅读(51)  评论(0)    收藏  举报