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 Delphi 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 :
className = class(BaseClass)
// Data/method defs local to this Unit
// Data/method defs local to this class + descendants
// Data/method defs usable with all objects of this class
// Externally interrogatable public definitions
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 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.
There are a number of uses of the word Class:
A class definition based on the TObject class by default. All classes must be based on another class, with TObject being the default, highest level class. TObject provides rudimentry functionality - you would normally define all of the data and methods yourself, and use none from TObject.
Within the definitions of a Class, you can prefix function or procedure definitions with the Class keyword. This allows the subroutine to be called from the Class itself in addition to an object instance of the class. Because a class is not a real object, it has no storage allocated to data. So a Class subroutine must operate without reference to internal class data.
Class definition based on a specified class but with no local additions or changes. You have a new class that acts identically to the BaseClass
Forward class declaration. It allows all classes in a Unit to be listed at the start of the type section. So this is a matter of convenience rather than anything else.
Class definition based on a specified class. As 1 above, but you can specify which class you are basing your new class on. Your class will again be a mixture of ancestor and local declarations.
A class can contain implementations of externally predefined interfaces.
A metaclass reference allows a variable to be used to reference a class rather than an object.
The optional packed keyword tells Delphi to minimise storage taken by the class. It stops aligning data on 2, 4 or 8 byte boundaries, normally done for performance reasons.|
||Defines the method used to create an object from a class
||Used for Unit external definitions, and as a Class skeleton
||Allows a subroutine data type to refer to an object method
||Starts the section of private data and methods in a class
||Defines controlled access to class fields
||Starts a section of class private data accesible to sub-classes
||Starts an externally accessible section of a class
||Starts a published externally accessible section of a class
||The base class type that is ancestor to all other classes
||Allows a class method to be overriden in derived classes
||Allows a class method to be overriden in derived classes
| Author links|
| Download this web site as a Windows program.
|Example code : A Unit with 2 class definitions and code that uses both|
// Full Unit code.|
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
SysUtils, Forms, Dialogs;
// Define the classes in this Unit at the very start for clarity
TForm1 = Class; // This is a forward class definition
TFruit = Class(TObject) // This is an actual class definition :
// Internal class field definitions - only accessible in this unit
isRound : Boolean;
length : single;
width : single;
diameter : single;
// Fields and methods only accessible by this class and descendants
// Externally accessible fields and methods
// 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
// Note that properties must use different names to local defs
property round : Boolean
property len : single
property wide : single
property diam : single
end; // End of the TFruit class definition
// The actual TForm1 class is now defined
TForm1 = Class(TForm)
procedure FormCreate(Sender: TObject);
procedure ShowFruit(fruit : TFruit);
// No local data
// Uses just the TForm ancestor class public definitions
// Create a round fruit object
constructor TFruit.Create(diameter: single);
// Indicate that we have a round fruit, and set its size
isRound := true;
self.diameter := diameter;
// Create a long fruit object
constructor TFruit.Create(length, width: single);
// Indicate that we have a long fruit, and set its size
isRound := false;
self.length := length;
self.width := width;
// Form object - action taken when the form is created
procedure TForm1.FormCreate(Sender: TObject);
apple, banana : TFruit;
// Let us create our fruit objects
apple := TFruit.Create(3.5);
banana := TFruit.Create(7.0, 1.75);
// Show details about our fruits
// Show what the characteristics of our fruit are
procedure TForm1.ShowFruit(fruit: TFruit);
then ShowMessage('We have a round fruit, with diam = '+
ShowMessage('We have a long fruit');
ShowMessage(' it has length = '+FloatToStr(fruit.len));
ShowMessage(' it has width = '+FloatToStr(fruit.wide));
We have a round fruit with diam = 3.5|
We have a long fruit
it has a length = 7
it has a width = 1.75