TRectangle = class(TFigure)
procedure Draw;
override;
end;
TEllipse = class(TFigure)
procedure Draw; override;
end;
Given these declarations, the following code illustrates the effect of calling a virtual method through a variable whose
actual type varies at runtime.
var
Figure: TFigure;
begin
Figure := TRectangle.Create;
Figure.Draw; // calls TRectangle.Draw
Figure.Destroy;
Figure := TEllipse.Create;
Figure.Draw; // calls TEllipse.Draw
Figure.Destroy;
end;
Only virtual and dynamic methods can be overridden. All methods, however, can be overloaded;
see Overloading
methods.
The Delphi for .NET compiler supports the concept of a
final virtual method. When the keyword final is applied to a
virtual method, no ancestor class can override that method. Use of the final keyword is an important design decision
that can help document how the class is intended to be used. It can also give the .NET JIT compiler hints that allow
it to optimize the code it produces.
Dostları ilə paylaş: