How to hide an inherited property in a class without modifying the inherited class (base class)?

前端 未结 10 1099
无人共我
无人共我 2020-11-27 20:21

If i have the following code example:

public class ClassBase
{
    public int ID { get; set; }

    public string Name { get; set; }
}

public class ClassA :         


        
10条回答
  •  Happy的楠姐
    2020-11-27 20:26

    Why force inheritance when it's not necessary? I think the proper way of doing it is by doing has-a instead of a is-a.

    public class ClassBase
    {
        public int ID { get; set; }
    
        public string Name { get; set; }
    }
    
    public class ClassA
    {
        private ClassBase _base;
    
        public int ID { get { return this._base.ID; } }
    
        public string JustNumber { get; set; }
    
        public ClassA()
        {
            this._base = new ClassBase();
            this._base.ID = 0;
            this._base.Name = string.Empty;
            this.JustNumber = string.Empty;
        }
    }
    

提交回复
热议问题