Quick start manual

Classes and objects
7-13
Methods
If you overload a virtual method, use the reintroduce directive when you redeclare it
in descendant classes. For example,
type
T1 = class(TObject)
procedure Test(I: Integer); overload; virtual;
end;
T2 = class(T1)
procedure Test(S: string); reintroduce; overload;
end;
ƒ
SomeObject := T2.Create;
SomeObject.Test('Hello!'); // calls T2.Test
SomeObject.Test(7); // calls T1.Test
Within a class, you cannot publish multiple overloaded methods with the same
name. Maintenance of runtime type information requires a unique name for each
published member.
type
TSomeClass = class
published
function Func(P: Integer): Integer;
function Func(P: Boolean): Integer // error
ƒ
Methods that serve as property read or write specifiers cannot be overloaded.
The implementation of an overloaded method must repeat the parameter list from
the class declaration. For more information about overloading, see “Overloading
procedures and functions” on page 6-8.
Constructors
A constructor is a special method that creates and initializes instance objects. The
declaration of a constructor looks like a procedure declaration, but it begins with the
word constructor. Examples:
constructor Create;
constructor Create(AOwner: TComponent);
Constructors must use the default register calling convention. Although the
declaration specifies no return value, a constructor returns a reference to the object it
creates or is called in.
A class can have more than one constructor, but most have only one. It is
conventional to call the constructor Create.
To create an object, call the constructor method on a class type. For example,
MyObject := TMyClass.Create;
This allocates storage for the new object on the heap, sets the values of all ordinal
fields to zero, assigns nil to all pointer and class-type fields, and makes all string
fields empty. Other actions specified in the constructor implementation are
performed next; typically, objects are initialized based on values passed as