When do we really need to invoke the parent's overridden method from within the overriding method?
When do we really need to invoke the parent's overridden method from within the child's overriding method?
namespace MvcMov开发者_运维技巧ie.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?
}
}
}
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.
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.
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.
public class Dinosaurs : Collection<string>
{
public event EventHandler<DinosaursChangedEventArgs> Changed;
protected override void InsertItem(int index, string newItem)
{
base.InsertItem(index, newItem);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Added, newItem, null));
}
}
...
}
Source
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.
public class Parent
{
protected IList<string> Names {get;set;}
public virtual void Addnames()
{
Names = new List<string>(){"A", "B"};
}
}
public class Child1 : Parent
{
public override void Addnames()
{
base.Addnames();
Names.Add("C");
}
}
public class Child2 : Parent
{
public override void Addnames()
{
Names = new List<string>(){"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.
精彩评论