从超类获取字段

杰斯珀

我有以下抽象类:

 abstract class Customer
{
    private string address { get; set; }
    private int phone { get; set; }

    public Customer(string address, int phone)
    {
        this.address = address;
        this.phone = phone;
    }
}

然后,我有以下从客户类继承的类:

   class Private : Customer
{

    private string name { get; set; }
    private int age { get; set; }
    private string sex { get; set; }

    public Private(string name, int age, string sex, string address, int phone) : base(address, phone)
    {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

我的问题是:如何访问我的私人班级的电话和地址字段?

费利佩·萨比诺(Felipe Sabino)

使用protected修饰符代替private继承的类可以访问的所有内容。

从文档中:

受保护的成员可以在其类中以及通过派生类实例进行访问。

https://msdn.microsoft.com/zh-CN/library/bcd5672a.aspx

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章