There are two main reasons for invoking an implementation of a base class in overriding one of its methods:
You want to extend the behavior of the base class.
. , , .
, , .
. , , , , .
, , , . , - . :
class Car{
bool engineRunning;
public virtual void StartEngine(){
TurnIgnitionKey();
engineRunning = true;
}
public void DriveAround(){
if(!engineRunning)
throw new InvalidOperationException("You have to start the engine first.");
}
}
class S2000 : Car{
public override void StartEngine(){
PushStartButton();
}
}
, S2000 StartEngine() Car, DriveAround() . Car , S2000, , , StartEngine.
Car . - :
class Car{
bool engineRunning;
public void StartEngine(){
StartEngineInternal();
engineRunning = true;
}
protected virtual void StartEngineInternal(){
TurnIgnitionKey();
}
}
, -, . , , , , , .