财产覆盖不在C#中工作?

时间:2017-04-09 06:32:56

标签: c# properties polymorphism override

在这里,我写了Special Customer classchild class和我 我希望特殊客户可以overridden Property Cname 仅在他们Cname="Special"时才更改名称,但目前不是 正在发生的是base Cname property正在检查余额 (我不想要Special Customer class) 如果我实现了,请告诉我 Runtime Polymorphism此代码

  class Customer
    {
       protected int _cid,_bal,_status;
        protected string _cnmae;

        public Customer(int _cid,int _bal,int _status,string _cname)
        {
            this._cid = _cid;
            this._bal = _bal;
            this._cnmae = _cname;
            this._status = _status;
         }
        public int Cid
        { //read only property
          get
            {return _cid;}

           }
        public virtual string Cname
        {
            get
            {return _cnmae;}
            set
            {
                if (_status != 0 & _bal >= 500)
                {_cnmae = value;}
            }
        }
        public int Bal
        {
            get
            {return _bal;}
            set
            {
                if (_status != 0 & value >= 500)
                { _bal = value;}
            }
        }
        public int Status
        {
            get{ return _status;}
            set
            {_status = value;}
        }
       public  virtual void display()
        {
          Console.WriteLine("id={0} and name={1} and balance={2} and status={3}", _cid, _cnmae,_bal,_status);
        }
  }

 class Specialcustomer:Customer
    {
        public Specialcustomer(int _cid, int _bal, int _status, string _cname) :base( _cid, _bal, _status,_cname)
        {
        }
        public override string Cname
        {
            get
            {return base.Cname}
          set
            {if (value == "SPECIAL")
                {
                    base.Cname = value;
                }
            }
        }

        public override void display()
        {
          Console.WriteLine("id={0} and name={1} and balance={2} and status={3}", _cid, _cnmae, _bal, _status);
        }
}
 class Program
    {        static void Main(string[] args)
        {
            Customer C1 = new Specialcustomer(10, 400, 1, "BOND");
            C1.display();
            C1.Cname = "SPECIAL";
            C1.display();
            Console.ReadKey();

        }
    }

1 个答案:

答案 0 :(得分:1)

您的代码已经正在运行 - 它正在调用SpecialCustomer.Cname setter,因为您可以通过在其中设置断点或添加一些日志记录来轻松判断。 (我刚刚在setter中添加了Console.WriteLine语句。)

但是,由于Cname setter中的条件,它不会更改Customer.Cname的值:

set
{
    if (_status != 0 & _bal >= 500)
    {
        _cnmae = value;
    }
}

如果您更改代码以使客户的余额(例如)为600而不是400,那么Cname会根据您的预期更改为SPECIAL

如果您希望SpecialCustomer无条件地更改名称​​ ,如果提供的值是SPECIAL,则需要将该功能添加到基类中,例如

class Customer
{
    public virtual string Cname
    {
        get { return _cnmae; }
        set
        {
            if (_status != 0 & _bal >= 500)
            {
                SetCnameUnconditionally(value);
            }
        }
    }

    // This method allows subclasses to bypass the conditions
    // in the normal setter
    protected void SetCnameUnconditionally(string value)
    {
        _cnmae = value;
    }
}

class SpecialClass
{
    public override string Cname
    {
        get { return base.Cname; }
        set
        {
            if (value == "SPECIAL")
            {
                SetCnameUnconditionally(value);
            }
        }
    }
}