覆盖C#中的属性不会发生 [英] Overriding not happening in property in C#

查看:63
本文介绍了覆盖C#中的属性不会发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cname =special

请让我知道我是否按时运行多态性?

请帮助我是C#的新手



 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
{//只读属性
get
{
return _cid;
}

}
公共虚拟字符串Cname
{
get
{
return _cnmae;
}
设置
{
if(_status!= 0& _bal> = 500)
{
_cnmae = value;
}
}
}
public int Bal
{
get
{
return _bal;
}
设置
{
if(_status!= 0& value> = 500)
{
_bal = value;
}
}
}
public int状态
{
get
{
return _status;
}
设定
{

_status = value;
}
}
public virtual void display()
{
// Console.WriteLine(id = {0} and name = {1} and balance = {2}和status = {3},Cid,Cname,Bal,Status);
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)
{}
公共覆盖字符串Cname
{
get
{
return base.Cname;

}

set
{
if(value ==Special)
{
base.Cname = value ;
}
}
}

公共覆盖void display()
{
//base.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 =特别;
C1.display();
Console.ReadKey();
}
}





我的尝试:



引用:

我创建了Specialcustomer类,我想让它更改名称,如果 cname =special

请告诉我,如果我按时运行多态性吗?

请帮助我我是C#的新手

解决方案

您需要一个基本属性或方法来设置_cnmae字段,而不用执行任何检查。如果您不希望类的导入者看到它,请将其保护。然后,在每个类的CName属性中,调用此受保护方法以在条件合适时设置值。即



  class 客户
{
protected int _cid,_bal,_status;
protected string _cnmae;


public 客户( int _cid, int _bal, int _status, string _cname)
{
this ._ cid = _cid;
._ bal = _bal;
this ._ cnmae = _cname;
._ status = _status;
}

public int Cid
{< span class =code-comment> // 只读属性
get
{
return _cid;
}

}

受保护 void SetCname( string value
{
_cnmae = < span class =code-sdkkeyword> value ;
}


public virtual string Cname
{
get
{
return _cnmae;
}
set
{
if (_status != 0 & _bal > = 500
{
SetCname( value );; }
}
}
public int Bal
{
获取
{
return _bal;
}
set
{
if (_status != 0 & value > = 500
{
_bal = value ;
}
}
}
public int 状态
{
get
{
return _status;
}
set
{

_status = value ;
}
}
public virtual void display()
{
// Console.WriteLine (id = {0}且name = {1}且balance = {2}且status = {3},Cid,Cname,Bal,Status);
Console.WriteLine( id = {0}且name = {1}且balance = {2}且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 覆盖 string Cname
{
get
{
返回 base .Cname;

}

set
{
if value == Special
{
base .SetCname( value ); }
}
}

public 覆盖 void display()
{
// < span class =code-comment> base.display();

Console.WriteLine( id = {0}且name = {1}且balance = {2}且status = {3},_ cid,_cnmae,_bal,_status);
}
}
class 计划
{
静态 void Main( string [] args)
{
Customer C1 = new Specialcustomer( 10 400 1 BOND);
C1.display();
C1.Cname = Special;
C1.display();
Console.ReadKey();
}
}


将基类属性更改为以下



公共虚拟字符串Cname 
{
get
{
return _cnmae;
}
设置
{
if(value ==Champ||(_status!= 0&& _bal> = 500))
{
_cnmae = value;
}
}
}


Quote:

here i have created Specialcustomer class in which i want it to change the name if cname="special"
PLEASE LET ME KNOW IF I AM DOING RUN TIME POLYMORPHISM BY THIS ?
pls help i am new to C#



   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,Cname,Bal,Status);
            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()
        {
            //base.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();
         }
    }



What I have tried:

Quote:

i have created Specialcustomer class in which i want it to change the name if cname="special"
PLEASE LET ME KNOW IF I AM DOING RUN TIME POLYMORPHISM BY THIS ?
pls help i am new to C#

解决方案

You need a base property or method that sets the _cnmae field without performing any checks. If you don't want it visible to the importers of the classes, make it protected. Then, in each of the class's CName property, call this protected method to set the value if or when the conditions are suitable. i.e.

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;
            }
            
        }

        protected void SetCname(string value)
        {
            _cnmae = value;
        }

        public virtual string Cname
        {
            get
            {
                return _cnmae;
            }
            set
            {
                if (_status != 0 & _bal >= 500)
                {
                    SetCname(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,Cname,Bal,Status);
            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.SetCname(value);                }
            }
        }
 
        public override void display()
        {
            //base.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();
         }
    }


Change base class property to below

public virtual string Cname
       {
           get
           {
               return _cnmae;
           }
           set
           {
               if (value == "Champ"|| (_status != 0 && _bal >= 500))
               {
                   _cnmae = value;
               }
           }
       }


这篇关于覆盖C#中的属性不会发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆