在为 ASP.NET 重写 C# 中的方法时,是否应该调用基类实现?
我了解重写方法/函数会根据基类中的实现重新定义其在派生类中的实现。
现在让我困惑的是,如果我重写 ASP.NET 中的一个类,例如 CreateChildControls() (我没有特殊原因随机选择它),VS2008 自动生成:
protected override void CreateChildControls()
{
base.CreateChildControls();
}
足够好了,默认实现只是调用基类的CreateChildControls()
。
因此,如果我想运行一些代码,因为我不知道如何 base.CreateChildControls()
,我应该这样做:
protected override void CreateChildControls()
{
/*My Code Here*/
base.CreateChildControls();
}
或者,完全忽略 base.CreateChildControls()
就做
protected override void CreateChildControls()
{
/*My Code Here*/
}
I understand overriding a method/function redefines its implementation in the derived class from its implementation in the base class.
Now what confuses me, is if I override a class in ASP.NET such as CreateChildControls()
(I picked it randomly for no particular reason), VS2008 auto generates:
protected override void CreateChildControls()
{
base.CreateChildControls();
}
Good enough, the default implementation just calls the base class' CreateChildControls()
.
So if I want to run some code, since I do not know how base.CreateChildControls()
, should I do this:
protected override void CreateChildControls()
{
/*My Code Here*/
base.CreateChildControls();
}
or, ignore what base.CreateChildControls()
altogether and just do
protected override void CreateChildControls()
{
/*My Code Here*/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这取决于你。 一般来说,您想要调用基类方法,因为它可能会做很多您不知道的事情(特别是在您无法控制的类中..明白了吗?控制。)但是,如果您非常有信心不需要(或希望)发生基类“东西”,您可以删除调用。
That's up to you. Generally, you want to call the base class method because it might do a lot of stuff you're not privy to (especially in a class you don't control .. get it? Control.) But, if you are very confident you don't need (or want) the base class "stuff" to happen, you can remove the call.
这只是一个你是否想要完全替换行为或添加行为的问题。
对于像 CreateChildControls 这样的东西,您可能会保留对基类的调用。
It's simply a question of whether you want to entirely replace the behavior or add behavior.
For something like CreateChildControls, you will probably retain the call to the base class.
这取决于你想做什么。 如果您希望调用 base.CreateChildControls() 方法,然后希望在调用该方法之前或之后执行一些自定义操作,那么您可以这样做。
如果您想完全控制调用 CreateChildControls 时发生的情况,那么您可以完全忽略调用它。
默认情况下它就在那里,这一事实只是为您提供一些指导。
it depends on what you want to do. If you want the base.CreateChildControls() method to be called and then you want to perform some custom action before or after the method is called, then you can do so.
If you want to have complete control of what is happening when CreateChildControls is called, then you can simply ignore calling it altogether.
The fact that it's in there by default is just a bit of guidance for you.
这取决于您是否想要替换或完成基本实现...在大多数情况下,您应该调用基本实现(并且在以下情况下您绝对应该这样做)
CreateChildItems
方法...)It depends if you want to replace or complete the base implementation... in most cases you should call the base implementation (and you definitely should do it in the case of the
CreateChildItems
method...)您可能想看看模板方法模式。 虽然您无法对库类的实现方式做任何事情,但它可能会帮助您确保您编写的代码易于正确使用。
You might want to take a look at the template method pattern. While you can't do anything about the way the library classes are implemented, it might help you ensure the code you write is easy to use correctly.