class
The Class keyword is the central part of Object Oriented code. It starts the definition
of literally a 'class' of object types.
This definition contains so-called 'members' - data and methods (subroutines). When an
object of the class is created, it is a stand alone item - you can access the data and
methods of the object independently of any other object. It is like a
record, but
with active components - methods.
These elements are defined in the class type definition. The method elements are implemented
in the implementation section of the Unit.
A class declaration has this typical basic layout:
type
className = class(BaseClass)
private
// Data/method defs local to this Unit
protected
// Data/method defs local to this class + descendants
public
// Data/method defs usable with all objects of this class
published
// Externally interrogatable public definitions
end;
Parameters can be passed when creating an object instance of a class. These are
passed to the Constructor method of the class. The word constructor is used
instead of function or method. You may have a number of constructors for different
parameter sets. These are supplied with the overload; keyword after the end of the
constructor definition.
Normally, the constructor method name is Create. See the code below for an example.
When an object is destroyed, the Destructor method is called. You can use this to
take special action before the object storage is reclaimed.
Normally, the destructor method name is Destroy.
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
Abstract,
Constructor,
Destructor,
Dynamic,
Interface,
Object,
Overload,
Private,
Property,
Protected,
Public,
Published,
TObject,
Virtual.