本文介绍了在C#重写的方法用于ASP.NET时,我应该调用基类的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知覆盖方法/函数重新定义它从它的基类实现派生类实现。



现在什么迷我,如果我重写一个类ASP.NET,如的CreateChildControls()(我把它捡起随机无特殊原因),VS2008自动生成:

 保护覆盖无效的CreateChildControls()
{
base.CreateChildControls();
}



够好,默认的实现只是调用基类的的CreateChildControls()



所以,如果我想运行一些代码,因为我不知道该怎么基地.CreateChildControls(),我应该这样做:

 保护覆盖无效的CreateChildControls()
{
/ *我的代码这里* /
base.CreateChildControls();
}

或无视 base.CreateChildControls()干脆只做

 保护覆盖无效的CreateChildControls()
{
/ *我的代码这里* /
}


解决方案

这取决于你。通常情况下,你要调用基类的方法,因为它可能会做很多东西你无法得知(特别是在你不控制类..怎么做呢?控制)。但是,如果你是非常相信你不需要(或希望)的基类的东西的出现,可以删除通话。


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*/                    
}
解决方案

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.

这篇关于在C#重写的方法用于ASP.NET时,我应该调用基类的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:58