考虑您有以下代码:
public abstract class MenuItem
{
protected string m_Title;
protected int m_Level;
protected MenuItem m_ParentItem;
public event ChooseEventHandler m_Click;
protected MenuItem(string i_Title, int i_Level, MenuItem i_ParentItem)
{
m_Title = i_Title;
m_Level = i_Level;
m_ParentItem = i_ParentItem;
}
}
和
public class ContainerItem : MenuItem
{
private List<MenuItem> m_SubMenuItems;
public ContainerItem(string i_Title, int i_Level, MenuItem i_ParentItem)
:base(i_Title, i_Level, i_ParentItem)
{
m_SubMenuItems = new List<MenuItem>();
}
public string GetListOfSubItems()
{
string subItemsListStr = string.Empty;
foreach (MenuItem item in m_SubMenuItems)
{
item.m_Title = "test"; // Cannot access protected member the qualifier
must be of type 'Ex04.Menus.Delegates.ContainerItem'
}
return subItemsListStr;
}
}
我真的不明白这个错误背后的逻辑,是的,我已经读过:http: //blogs.msdn.com/b/ericlippert/archive/2005/11/09/491031.aspx
但我仍然认为它完全不合逻辑根据受保护的访问修饰符的定义。我认为它应该可以从定义它的同一个类访问,该类是它MenuItem
的所有派生类!(ContainerItem
等)m_Title
在持有对MenuItem
(由于多态性设计原因)的引用时,您将如何访问受保护的成员?