在方法开始或结束时调用基本方法?
我发现的唯一类似的问题是 this 和答案建议必须使用 Reflector 来找出答案。
那么在大多数情况下呢?一般来说,基方法是在方法中首先调用还是最后调用?
我注意到在一些库中它是在方法的开头调用的,而在 XNA Framework 中它们是在方法的末尾调用的(base.Update、base.Draw 等)。
The only similar question to this I found was this and the answer suggested having to use Reflector to find out.
What about in just the majority of the cases? Generally, is the base method called first or last in a method?
I noticed in some libraries it is called at the beginning at the method and in the XNA Framework they're called at the end of methods (base.Update, base.Draw, et cetera).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您希望派生行为发生在基本行为之前还是之后。
并且不要忘记您可以在中间调用基本方法或根本不调用。
话虽如此,一般来说都会被称为第一件事。因为这样您的重写方法就可以选择“覆盖”基类完成的设置。
但在像 Close 或 Dispose 这样的方法中,更习惯的是(有时是强制性的)最后调用它。
It depends on whether you want you derived behaviour to happen before or after the base behaviour.
And don't forget you can call the base method in the middle or not at all.
Having said that, in general it will be called as the first thing. Because then your overriding method has the option to 'overwrite' settings done by the base-class.
But in methods like Close or Dispose it is more customary (sometimes mandatory) to call it in the end.
这完全取决于你想做什么。关于应该发生什么,并没有真正的“一般”规则。例如,您可能想要进行一些额外的验证,然后调用基本方法,然后执行其他操作。或者也许您只是想计算调用基本方法所需的时间。
将每个案例视为个别情况。
It entirely depends on what you want to do. There's not really a "general" rule about what should happen. For example, you might want to do some extra validation, then call the base method, then do something else. Or maybe you just want to time how long the base method takes to call.
Treat each case as an individual situation.