Quick start manual

Classes and objects
7-7
Fields
Forward declarations allow mutually dependent classes. For example,
type
TFigure = class; // forward declaration
TDrawing = class
Figure: TFigure;
ƒ
end;
TFigure = class // defining declaration
Drawing: TDrawing;
ƒ
end;
Do not confuse forward declarations with complete declarations of types that derive
from TObject without declaring any class members.
type
TFirstClass = class; // this is a forward declaration
TSecondClass = class // this is a complete class declaration
end;//
TThirdClass = class(TObject); // this is a complete class declaration
Fields
A field is like a variable that belongs to an object. Fields can be of any type, including
class types. (That is, fields can hold object references.) Fields are usually private.
To define a field member of a class, simply declare the field as you would a variable.
All field declarations must occur before any property or method declarations. For
example, the following declaration creates a class called TNumber whose only
member, other than the methods is inherits from TObject, is an integer field called Int.
type TNumber = class
Int: Integer;
end;
Fields are statically bound; that is, references to them are fixed at compile time. To
see what this means, consider the following code.
type
TAncestor = class
Value: Integer;
end;
TDescendant = class(TAncestor)
Value: string; // hides the inherited Value field
end;
var
MyObject: TAncestor;
begin
MyObject := TDescendant.Create;
MyObject.Value := 'Hello!'; // error
(MyObject as TDescendant).Value := 'Hello!'; // works!
end;