无法从C#中的派生类访问受保护的字段

时间:2015-02-24 12:10:57

标签: c# asp.net

我尝试从派生类访问受保护的字符串,但无法访问

public class ParentClass
    {
        public string UserName {
            get { return "User Name is accessible to any class under same namespace Skin_File"; }
            set { }
        }
        private string AccountNumber {
            get { return "Not accessible from child class"; }
            set { }
        }
        internal string UserAge {
            get { return "User Age is accesible to child class"; }
            set { }
        }
        protected string PhoneNumber {
            get { return "Accessible only to ParentClass"; }
            set { }
        }
    }
    protected class DerivedClass : ParentClass
    {
        string DerivedUserName = new ParentClass().UserName;
        string  DerivedAccountNumber = new ParentClass().AccountNumber; // Not accessible since that was private
        string DerivedUserAge = new ParentClass().UserAge;
        protected  string DerivedPhoneNumber = new ParentClass().PhoneNumber; // derived class but not accessible
    }

以下是需要它的人的fiddle

3 个答案:

答案 0 :(得分:7)

此:

string DerivedPhoneNumber = new ParentClass().PhoneNumber;

从中创建ParentClass的实例,并且只能访问公共成员。当您开始ParentClass时,继承其受保护的成员。

如果要初始化值,则需要在类构造函数中执行此操作:

protected DerivedClass()
{
    DerivedPhoneNumber = PhoneNumber;
}

虽然,我没有看到声明另一个字段存储相同数字的重点。直接访问它。

答案 1 :(得分:3)

您创建ParentClass的实例。您无法看到受保护的成员。 您可以从DerivedClass访问它的受保护成员。 你必须只是调用

protected  string DerivedPhoneNumber = PhoneNumber;

尝试以下

internal class DerivedClass : ParentClass
{
    string DerivedUserName = new ParentClass().UserName;
    string DerivedUserAge = new ParentClass().UserAge;
    protected string DerivedPhoneNumber;// = PhoneNumber; // derived class but not accessible

    public DerivedClass() {
        DerivedPhoneNumber = PhoneNumber;
    }
}

答案 2 :(得分:1)

您不应该为所谓的访问创建一个新类,它会完全破坏继承。

只能在声明类中访问私有字段。 可以通过声明类+所有派生类+扩展方法来访问受保护的字段 可以通过在同一名称空间/程序集(我认为)

中声明类+所有派生类+扩展方法来访问内部字段

此外,在考虑基类/派生类时,请考虑修饰符,您希望允许基类修改的属性/方法应标记为虚拟,不应进一步扩展的属性/方法,不应该得到修饰语。

所以,基类:

public class ParentClass
{
    private string userName;
    public virtual string UserName
    {
        get
        {
            return username;
        }
        set
        {
            username = value;
        }
    }

    private string accountNumber; // don't see a reason why it should be a property rather than a field...

    private int userAge;
    internal int UserAge
    {
        get
        {
            return userAge;
        }
        set
        {
            userAge = value;
        }
    }

    protected string phoneNumber;
    public virtual string PhoneNumber
    {
        get
        {
            return phoneNumber;
        }
        set
        {
            phoneNumber = value;
        }
    }

    public ParentClass()
    {
        // potentially do something intresting
    }
}

现在让它变成多态的

public class DerivedClass : ParentClass
{
    public string CountryCode
    {
        get; set;
    }
    // we can override phoneNumber
    public override string PhoneNumber
    {
        get
        {
            if (!string.IsNullOrWhiteSpace(base.PhoneNumber)) {
                 return base.PhoneNumber;
            }
            if (!base.PhoneNumber.StartsWith(CountryCode)) {
                return CountryCode + base.PhoneNumber;
            }
            return base.PhoneNumber;  // you could also return the field phoneNumber instead (as it is protected)
        }
        set
        {
            base.PhoneNumber = CountryCode + value; // you could also set the field phoneNumber ... (we are adding country code here)
        }
    }

    public DerivedClass() : this("0049")
    {
    }

    public DerivedClass(string countryCode) : base() // call the base constructor as well
    {
        // do something more than what the parentclass is doing, eg...
         this.CountryCode = countryCode;
    }
}

现在,在Derivedclass中你可以选择以不同的方式设置/返回phonenumber,这意味着所有看到ParentClass的类也可以使用DerivedClass,并具有相同的属性,但是该属性的实现会由派生类改变。

然而,你的例子对我来说似乎不够清楚,知道你的要求是什么,你想做什么,以及它应该如何表现。