C# 重写和重写方法必须具有相同的可访问性,那么为什么isn’;对于Page.CreateChildControls(),情况不一样吗?
标记为C# 重写和重写方法必须具有相同的可访问性,那么为什么isn’;对于Page.CreateChildControls(),情况不一样吗?,c#,.net,asp.net,inheritance,C#,.net,Asp.net,Inheritance,标记为virtual的方法可以在派生类中重写。限制之一是重写和重写方法必须具有相同的可访问性。因此,如果虚拟方法标记为受保护的内部,则重写方法也必须标记为受保护的内部(例如,它不能仅标记为受保护的) 由于Page类重写了标记为protected internal的Control.CreateChildControls(),因此Page.CreateChildControls()也应标记为protected internal,但改为标记为protected。这怎么可能呢?我可能没把你的问题答对。这
virtual
的方法可以在派生类中重写。限制之一是重写和重写方法必须具有相同的可访问性。因此,如果虚拟方法标记为受保护的内部
,则重写方法也必须标记为受保护的内部
(例如,它不能仅标记为受保护的
)
由于
Page
类重写了标记为protected internal
的Control.CreateChildControls()
,因此Page.CreateChildControls()
也应标记为protected internal
,但改为标记为protected
。这怎么可能呢?我可能没把你的问题答对。这是我在MSDN上找到的
可能是因为您在MSDN上看到的示例不正确:
protected override void CreateChildControls()
{
// Creates a new ControlCollection.
this.CreateControlCollection();
// Create child controls.
ChildControl firstControl = new ChildControl();
firstControl.Message = "FirstChildControl";
ChildControl secondControl = new ChildControl();
secondControl.Message = "SecondChildControl";
Controls.Add(firstControl);
Controls.Add(secondControl);
// Prevent child controls from being created again.
ChildControlsCreated = true;
}
来源:嗯,我可以发誓我在MSDN页面上看到类使用just protected修饰符定义CreateChildControls()。我今天老是把事情搞砸你看到的都是对的。方法签名和示例是相互矛盾的。我也对它感到困惑。我可能会发布一个新问题。结果我贴了一张新的海报,上面有更多的解释。是的,有一刻我以为我在做梦。Thanx mate
protected override void CreateChildControls()
{
// Creates a new ControlCollection.
this.CreateControlCollection();
// Create child controls.
ChildControl firstControl = new ChildControl();
firstControl.Message = "FirstChildControl";
ChildControl secondControl = new ChildControl();
secondControl.Message = "SecondChildControl";
Controls.Add(firstControl);
Controls.Add(secondControl);
// Prevent child controls from being created again.
ChildControlsCreated = true;
}