Override get, But not set

前端 未结 13 2449
暖寄归人
暖寄归人 2020-12-14 14:17

I have an abstract class that defines a get, but not set, because as far as that abstract class is concerned, it needs only a get.

13条回答
  •  遥遥无期
    2020-12-14 14:51

    public abstract class BaseClass
    {
        public abstract double MyPop { get; }
    }
    
    public class DClass: BaseClass
    {
        private double _myPop = 0;
        public override double MyPop 
        {
            get { return _myPop; }
        }
    
        // some other methods here that use the _myPop field
    }
    

    If you need to set the property from outside DClass then maybe it would be better to put the setter into the base class.

提交回复
热议问题