简体   繁体   English

错误:由于其保护级别而无法访问

[英]Error: Inaccessible due to its protection level

This is an example from MSDN which is from the portion explaining 'protected'member access modifier. 这是来自MSDN的示例,该示例来自解释“受保护”成员访问修饰符的部分。 My question is, why I am getting compile error, if I modifying this program like in Example II, 我的问题是,如果像示例II中那样修改该程序,为什么会出现编译错误,

Example I 例子一

class A
{
    protected int x = 123;
}

class B : A
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        b.x = 10;
    }
}

Example II 例二

class A
{
    protected int x = 123;
}

//MODIFICATION IN BELOW 2 LINES
class B : A{}
class program
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        b.x = 10;
    }
}

Compiler Error for Example II : 示例II的编译器错误:

d:\MyProgs>csc _13protected.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

_13protected.cs(14,15): error CS0122: 'A.x' is inaccessible due to its
        protection level
_13protected.cs(3,23): (Location of symbol related to previous error)

d:\MyProgs>

protected means it's not visible outside of the class itself, only in the class itself or derived classes. protected意味着它在类本身之外是不可见的,只有在类本身或派生类中才可见。

In your first example it works because your main method is part of the derived class. 在您的第一个示例中,它起作用是因为您的main方法是派生类的一部分。

In your second example, your are trying to access a protected member outside of its class, which is not possible. 在第二个示例中,您试图访问其类之外的受保护成员,这是不可能的。 If you want to make this possible, x should be declared public . 如果您想做到这一点,则应将x声明为public

See http://msdn.microsoft.com/en-us/library/bcd5672a.aspx for more information about what protected means. 有关protected含义的更多信息,请参见http://msdn.microsoft.com/zh-cn/library/bcd5672a.aspx

Read the definition of access modifiers and you will get your answer. 阅读访问修饰符的定义,您将得到答案。

public: The type or member can be accessed by any other code in the same assembly or another assembly that references it. public:类型或成员可以被同一程序集或引用它的另一个程序集中的任何其他代码访问。

private: The type or member can be accessed only by code in the same class or struct. 私有:类型或成员只能由相同类或结构中的代码访问。

protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class. protected:类型或成员只能由相同类或结构或从该类派生的类中的代码访问。

internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. 内部:类型或成员可以由同一程序集中的任何代码访问,但不能从另一个程序集访问。

You need to pay attention to actual classes where access to protected field happens. 您需要注意发生实际访问受保护字段的类。

In second example you are trying to access bX from the program class, which is not allowed by "protected" modifier, as program class doesn't inherit from A. 在第二个示例中,您尝试从程序类访问bX,这是“ protected”修饰符所不允许的,因为程序类不继承自A。

On the other hand, In first example actuall access to bX happened in B class, which inherits from A, therefore access is allowed by "protected" modifier. 另一方面,在第一个示例中,对bX的实际访问发生在B类中,该类继承自A,因此“保护”修饰符允许访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM