美文网首页
C# Protected

C# Protected

作者: func_老衲姓罗 | 来源:发表于2017-08-16 17:30 被阅读0次

The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances.
Example
A protected member of a base class is accessible in a derived class only if the access occurs through the derived class type. For example, consider the following code segment:
C#
class A
{
protected int x = 123;
}

class B : A
{
static void Main()
{
A a = new A();
B b = new B();
// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;
// OK, because this class derives from A.
b.x = 10;
}
}

相关文章

网友评论

      本文标题:C# Protected

      本文链接:https://www.haomeiwen.com/subject/mrtwrxtx.html