为什么我没有使用“受保护”?工作?
我读过可以从派生类访问受保护的成员,但以下内容不起作用。
class A
{
protected int Test;
}
class B:A
{
A instanceOfA= new A()
public B()
{
instanceOfA.Test //Not possible
}
}
I have read that a protected member can be accessed from derived classes, but the following does not work.
class A
{
protected int Test;
}
class B:A
{
A instanceOfA= new A()
public B()
{
instanceOfA.Test //Not possible
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是非法的,因为您没有保证您正在访问“B”实例的数据。考虑类似的情况:
EvilBankAccount 不继承自 SwissBankAccount,因此不允许在 EvilBankAccount 内部使用 SwissBankAccount 的受保护成员。您可以访问“父母”的受保护成员,但不能访问“兄弟姐妹”! EvilBankAccount 只能访问 EvilBankAccount 的受保护成员(或从 EvilBankAccount 派生的类型)。 SwissBankAccount 的受保护会员不受限制。
规则是受保护实例成员访问所通过的“接收者”表达式的类型必须至少与包含成员访问的类型声明一样派生。有关规则的精确措辞和一些说明性示例,请参阅 C# 4.0 规范的第 3.5.3 节。
顺便说一句,C++也有这个规则。
这条规则经常被误解。要更深入地分析此规则以及受保护访问的其他一些后果,请参阅我有关该主题的文章。我写过的关于这个主题的最切题的文章是 这个 和 这个。我在此处撰写了更多有关相关主题的文章(尽管其中一些人偏离了受保护访问本身的主题,而转向了如何使用受保护访问来构建具有父引用的数据模型的主题。)
This is illegal because you have not provided a guarantee that you are accessing data of an instance of "B". Consider this similar case:
EvilBankAccount does not inherit from SwissBankAccount, so SwissBankAccount's protected member is not allowed to be used inside EvilBankAccount. You're allowed to access the protected members of your "parents", but not your "siblings"! EvilBankAccount can only access protected members of EvilBankAccount (or a type derived from EvilBankAccount). SwissBankAccount's protected members are off-limits.
The rule is that the type of the "receiver" expression that the protected instance member is accessed through has to be at least as derived as the type declaration containing the member access. For the precise wording of the rule and some illustrative examples see section 3.5.3 of the C# 4.0 specification.
Incidentally, C++ also has this rule.
This rule is frequently misunderstood. For a more in-depth analysis of this rule and some of the other consequences of protected access, see my articles on the subject. The most germane articles I've written on this subject are this one and this one. There are a bunch more articles I've written on related topics here (though some of them wander off the topic of protected access itself and onto the topic of how to use protected access to build a data model with parent references.)
您尚未正确设置代码。类
B
不应具有类A
的实例。相反,类B
本身继承了类A
的受保护变量。您的代码应该看起来更像:
You haven't set up your code correctly. Class
B
shouldn't have an instance of classA
. Instead classB
itself inherits the protected variables from classA
.Your code should look more like:
您可以在
B
类本身中访问Test
int。但是,您无法访问实例的属性。A
不知道它是B
的子级,因此不会授予对其属性的访问权限。You can access the
Test
int within theB
class itself. However, you cannot access properties of an instance.A
does not know that it is a child ofB
so won't give access to its property.由于
B
已经继承自A
,因此您不需要单独的A
实例。As
B
already inherits fromA
, you don't need a separate instance ofA
.您可以通过同一个类而不是作为基类的公共成员来访问它。
检查 C# 中的 受保护 访问修饰符。
You can access to it through the same class not as a public member of the base class.
Check protected access modifier in C#.
子类可以访问自己标记为
受保护
的继承成员。Subclasses can access their own inherited members that are marked as
protected
.如果继承
A
,则不需要创建A
的实例。You do not need to create an instance of
A
if you inheritA
.