【发布时间】:2011-12-08 06:13:07
【问题描述】:
查看代码中的问题:
class commonParent {
protected string name;
}
class child1:commonParent{
// do some stuff
}
class child2:commonParent{
// do some stuff
protected void test(){
child1 myChild1 = new child1();
//is it possible to access myChild1.name in child2 without
//declaring the name public or internal?
// I want to do something like this:
string oldName = myChild1.name;
//but I got the error:
//Error 46 Cannot access protected member 'commonParent.name'
//via a qualifier of type 'child1'; the qualifier must be of
//type 'child2' (or derived from it)
}
}
字段“name”仅由 commonParent 类的所有子级使用。我想从外部隐藏这个字段(不是从 commonParent 派生的类),同时让它在 commonParent 及其子项的范围内可访问。
【问题讨论】:
-
当您尝试访问 myChild1.name 时是否出现任何错误?
-
是的,我遇到了这个错误:错误 46 无法通过“child1”类型的限定符访问受保护的成员“commonParent.name”;限定符必须是“child2”类型(或派生自它)