The virtual and override Keywords
Polymorphism provides a way for a subclass to define its own version of a method defined by its
base class, using the process termed
method overriding. To retrofit your current design, you need to
understand the meaning of the virtual and override keywords. If a base class wishes to define a
method that
may be (but does not have to be) overridden by a subclass, it must mark the method
with the virtual keyword:
partial class Employee
{
// This method can now be 'overridden' by a derived class.
public virtual void GiveBonus(float amount)
C H A P T E R 6
■
U N D E R S TA N D I N G I N H E R I TA N C E A N D P O LY M O R P H I S M
199
8849CH06.qxd 10/1/07 10:35 AM Page 199
{
currPay += amount;
}
...
}
Do'stlaringiz bilan baham: |