Override get, But not set

前端 未结 13 2469
暖寄归人
暖寄归人 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:45

    If BaseClass is in your own codebase, then you can do:

    abstract public class BaseClass
    {
        abstract public double MyPop { get; protected set; }
    }
    
    public class DClass : BaseClass
    {
        private double _myProp;
        public override double MyProp
        {
            get { return _myProp; }
            protected set { _myProp = value; }
        }
    }
    

    EDIT: You can then go make a public method in DClass SetMyProp(double myProp) or the like. The class design for your domain model should be clear about or speak for itself why you can't set the property directly in the base class and why you can do so in the derived one.

提交回复
热议问题