Quick start manual

7-10
Delphi Language Guide
Methods
Self is useful for a variety of reasons. For example, a member identifier declared in a
class type might be redeclared in the block of one of the class’s methods. In this case,
you can access the original member identifier as Self.Identifier.
For information about Self in class methods, see “Class methods” on page 7-26.
Method binding
Method bindings can be static (the default), virtual, or dynamic. Virtual and dynamic
methods can be overridden, and they can be abstract. These designations come into
play when a variable of one class type holds a value of a descendant class type. They
determine which implementation is activated when a method is called.
Static methods
Methods are by default static. When a static method is called, the declared (compile-
time) type of the class or object variable used in the method call determines which
implementation to activate. In the following example, the Draw methods are static.
type
TFigure = class
procedure Draw;
end;
TRectangle = class(TFigure)
procedure Draw;
end;
Given these declarations, the following code illustrates the effect of calling a static
method. In the second call to Figure.Draw, the Figure variable references an object of
class TRectangle, but the call invokes the implementation of Draw in TFigure, because
the declared type of the Figure variable is TFigure.
var
Figure: TFigure;
Rectangle: TRectangle;
begin
Figure := TFigure.Create;
Figure.Draw; // calls TFigure.Draw
Figure.Destroy;
Figure := TRectangle.Create;
Figure.Draw; // calls TFigure.Draw
TRectangle(Figure).Draw; // calls TRectangle.Draw
Figure.Destroy;
Rectangle := TRectangle.Create;
Rectangle.Draw; // calls TRectangle.Draw
Rectangle.Destroy;
end;