我们什么时候真正需要从重写方法中调用父级的重写方法?
我们什么时候真正需要从子级的重写方法中调用父级的重写方法?
namespace MvcMovie.Models
{
public class MovieInitializer: DropCreateDatabaseIfModelChanges<MovieDbContext>
{
protected override void Seed(MovieDbContext context)
{
base.Seed(context);// is it necessary to invoke this parent's method here?
}
}
}
When do we really need to invoke the parent's overridden method from within the child's overriding method?
namespace MvcMovie.Models
{
public class MovieInitializer: DropCreateDatabaseIfModelChanges<MovieDbContext>
{
protected override void Seed(MovieDbContext context)
{
base.Seed(context);// is it necessary to invoke this parent's method here?
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 CLR 的角度来看,这不是必需的。当您想要或需要调用它时,它完全取决于所涉及的类以及方法的作用。
It's not required from the perspective of the CLR. When you want or need to invoke it depends entirely upon the classes involved and what the methods do.
直白的答案是——“你永远不知道何时调用基类方法”。
你还可以质疑基类方法的调用顺序?
如在派生类实现之前或之后!
我问过类似的问题,请查看此处。
在我看来,基类不应该期望派生类调用它的方法。我的意思是,API 应该以这种方式设计。
因为,如果基类期望其用户(派生类)回调其方法(在派生类实现之前或之后),那么它实际上是在对用户进行假设。这确实是一个糟糕的 API 设计。
希望它会有所帮助。
The blunt answer is - "you never know when to call the base class method".
You can also question the order in which the base class method is to be called?
as in, before or after the derived class implementation!!
I have asked a similar question, take a look here.
In my opinion, the base class should not expect the derive class to call its method. I mean, the API should be designed in that fashion.
Since, if base class expects its users (derived classes) to call its method back (before or after the derived class implementation), then it is actually making assumption about the users. Which indeed is a bad API design.
Hope it would be of help.
如果是给定的示例,则根本没有理由重写该方法,因为您没有添加任何内容。
您想要重写并调用基本方法的一个示例是,如果您正在扩展
Collection
但您希望在添加或删除对象时触发事件。来源
If it is the given example, there is no reason to override the method at all because you are not adding anything.
An example of of when you would want to override and call the base method would be if you were extending
Collection<T>
but you want to trigger events when objects are added or removed.Source
当您希望父级的方法和子级的方法一样运行时,
例如:子级 1 在调用父级方法时,其列表中将包含 A、B 和 C,但子级 2 将只有 X、Y Z。
您通常会这样做当您希望基类执行一般功能并且子类添加该功能时。希望有帮助
When you want the parent's method to run as well as the child's
for example: Child 1 will have A, B and C in its list as it calls the parent's method but Child 2 will only have X, Y Z.
You would generally do this when you want the base class to perform general functions and the child class adds to that. Hope that helps
在我看来,总是这样,或者这是您没有正确使用基类的标志。
基类的目的是集中类之间的通用功能。因此,基本方法的重点是提供这些类之间通用的基本行为。
如果您要实现的方法应该在不同的类上具有共同的签名,但没有共同的行为,那么您应该实现一个接口。
编辑:为了澄清,基方法应该始终承担所有派生类应该完成的工作。派生类应该只添加该行为。
In my opinion, always or it's a sign you're not using base classes properly.
The point of base classes is to centralize common functionality between classes. The point of the base methods is therefore to provide base behaviour that is common between these classes.
If you're implementing methods that should have a common signature over different classes, but without common behaviour, you should be implementing an interface.
EDIT: To clarify, a base method should always be doing the share of the work that should be done by all derived classes. The derived classes should only ADD to that behaviour.