财产将如何与继承一起使用? [英] How will property works with inheritance ?

查看:80
本文介绍了财产将如何与继承一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有这个例子:

lets say i have this eg:

class customer
   {
       int _custid;
       bool _status;
       string _Cname;
       double _balance;

       public customer(int custid, bool status, string Cname, double balance)
       {
           this._custid = custid;
           this._status = status;
           this._Cname = Cname;
           this._balance = balance;

       }
       public int Custid   //get only property
       {
           get
           {return _custid;}
        }
       public bool status
       {
           get { return _status; }
           set { _status = value; }
       }
       public string Cname
       {
           get { return _Cname;}
           set {
               if(_status==true)
               _Cname = value;
           }
       }
       public double balance
       {
           get { return _balance; }
           set {if (_status==true  & value>=500)
               _balance = value; }

       }
   }




 class specialcustomer :customer
    {

//here i want my special customer which can change the name even though there bal is //less than 500 but i don't know how to do it !!       
    }







class Program
   {
       static void Main(string[] args)
       {
           customer C = new customer(100, false, "AKHIL", 2000.00);
           Console.WriteLine("customer id={0}",C.Custid);
           Console.WriteLine("customer name={0}", C.Cname);
           C.Cname = "ramu";
           Console.WriteLine("customer changed name={0}", C.Cname);
           Console.WriteLine("customer status={0}", C.status);
           Console.WriteLine("customer balance={0}", C.balance);
           C.balance = 300;
           Console.WriteLine("customer changed balance={0}", C.balance);
           Console.ReadLine();
       }
   }





我尝试了什么:





What I have tried:

引用:

这里我希望我的特殊客户可以更改名称,即使bal少于500但我不知道怎么做!!

here i want my special customer which can change the name even though there bal is less than 500 but i don't know how to do it !!

推荐答案

这是一种方式......

Here is one way...
public class customer
{
    int _custid;
    bool _status;
    string _Cname;
    double _balance;

    public customer(int custid, bool status, string Cname, double balance)
    {
        this._custid = custid;
        this._status = status;
        this._Cname = Cname;
        this._balance = balance;
    }

    // trimmed for example

    public string Cname
    {
        get { return _Cname; }
        set { SetName(value); }
    }

    internal virtual void SetName(string value)
    {
        if (_status == true)
            _Cname = value;
    }

    // trimmed for example
}

public class specialcustomer : customer
{
    public specialcustomer(int custid, bool status, string Cname, double balance)
        : base(custid, status, Cname, balance)
    {
    }

    internal override void SetName(string value)
    {
        var tmpStatus = status;
        status = true;
        base.SetName(value);
        status = tmpStatus;
    }
}


执行此操作的方法是使CName属性为virtual,并保护_CName字段。



The way to do this is to make the CName property virtual, and the _CName field protected.

public class customer {
    protected string _CName;
    public virtual string CName {
        get { return _CName; }
        set {
            if (status)
                _CName = value;
        }
    }
}
public class specialcustomer {
     public override string CName {
        get { return base.CName; }
        set { _CName = value; }
     }
}


这篇关于财产将如何与继承一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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