这是一个来自 MSDN 的示例,来自解释“受保护”成员访问修饰符的部分。我的问题是,如果我像示例 II 中那样修改这个程序,为什么会出现编译错误,
例一
class A
{
protected int x = 123;
}
class B : A
{
static void Main()
{
A a = new A();
B b = new B();
b.x = 10;
}
}
例二
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;
}
}
示例 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>