Quick start manual

Classes and objects
7-9
Methods
Method declarations can include special directives that are not used with other
functions or procedures. Directives should appear in the class declaration only, not in
the defining declaration, and should always be listed in the following order:
reintroduce; overload; binding; calling convention; abstract; warning
where binding is virtual, dynamic, or override; calling convention is register, pascal,
cdecl, stdcall, or safecall; and warning is platform, deprecated, or library.
Inherited
The reserved word inherited plays a special role in implementing polymorphic
behavior. It can occur in method definitions, with or without an identifier after it.
If inherited is followed by the name of a member, it represents a normal method call
or reference to a property or field—except that the search for the referenced member
begins with the immediate ancestor of the enclosing method’s class. For example,
when
inherited Create(...);
occurs in the definition of a method, it calls the inherited Create.
When inherited has no identifier after it, it refers to the inherited method with the
same name as the enclosing method or, if the enclosing method is a message handler,
to the inherited message handler for the same message. In this case, inherited takes
no explicit parameters, but passes to the inherited method the same parameters with
which the enclosing method was called. For example,
inherited;
occurs frequently in the implementation of constructors. It calls the inherited
constructor with the same parameters that were passed to the descendant.
Self
Within the implementation of a method, the identifier Self references the object in
which the method is called. For example, here is the implementation of TCollection’s
Add method in the Classes unit.
function TCollection.Add: TCollectionItem;
begin
Result := FItemClass.Create(Self);
end;
The Add method calls the Create method in the class referenced by the FItemClass
field, which is always a TCollectionItem descendant. TCollectionItem.Create takes a
single parameter of type TCollection, so Add passes it the TCollection instance object
where Add is called. This is illustrated in the following code.
var MyCollection: TCollection;
ƒ
MyCollection.Add // MyCollection is passed to the TCollectionItem.Create method