constructor
The Constructor keyword defines a constructor procedure Name for a class.
When creating an object, you call a Constructor method of the class, not the object:
objectName := ClassName.Create(parms);
The Name for the constructor is normally Create, but is not restricted to this. It
is very wise to keep to this naming.
An object may be constructed with or without arguments (see the examplei below).
Constructors may be defined in the public or published sections of the class definition.
You may have multiple constructors, but by doing so, you can only define one of these as
Published. With multiple constructors, each must be suffixed with the Overload directive.
When implementing the constructor procedure, normally called Create, you should make it a
habit of calling the parent constructor, for example
constructor Create;
inherited;
...
This ensures that the resulting object is a safe instantiated instance of this parent class,
even if the parent is TObject, which does nothing in its constructor. The sample code
illustrates this plain variety of Inherited plus the version where the parent constructor
has arguments.
Example
Download This Source for Free Pascal
{$MODE DELPHI} // Enable Class support
type
TFruit = Class(TObject) // This is an actual class definition :
// Internal class field definitions - only accessible in this unit
private
isRound : Boolean;
length : single;
width : single;
diameter : single;
// Fields and methods only accessible by this class and descendants
protected
// Externally accessible fields and methods
public
// 2 constructors - one for round fruit, the other long fruit
constructor Create(diameter : single); overload;
constructor Create(length : single; width : single); overload;
// Externally accessible and inspectable fields and methods
published
// Note that properties must use different names to local defs
property round : Boolean read isRound;
property len : single read length;
property wide : single read width;
property diam : single read diameter;
end; // End of the TFruit class definition
var
apple, banana : TFruit;
// Create a round fruit object
constructor TFruit.Create(diameter: single);
begin
// Indicate that we have a round fruit, and set its size
isRound := true;
self.diameter := diameter;
end;
// Create a long fruit object
constructor TFruit.Create(length, width: single);
begin
// Indicate that we have a long fruit, and set its size
isRound := false;
self.length := length;
self.width := width;
end;
// Show what the characteristics of our fruit are
procedure ShowFruit(fruit: TFruit);
begin
if fruit.round then Writeln('We have a round fruit, diameter = ',fruit.diam)
else begin
Writeln('We have a long fruit');
Writeln(' it has length = ',fruit.len);
Writeln(' it has width = ',fruit.wide);
end;
end;
Begin
// Let us create our fruit objects
apple := TFruit.Create(3.5);
banana := TFruit.Create(7.0, 1.75);
// Show details about our fruits
ShowFruit(apple);
ShowFruit(banana);
end.
Output
We have a round fruit, diameter = 3.500000000E+00
We have a long fruit
it has length = 7.000000000E+00
it has width = 1.750000000E+00
See Also
Class,
Destructor,
Function,
Inherited,
Object,
Procedure,
TObject.