属性重写在 C# 中不起作用



在这里我写了Special Customer class 这是 child class 和 i有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();
        }
    }

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

但是,它不会因为Customer.Cname资源库中的条件而更改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);
            }
        }
    }
}

最新更新