Class Types

A class, or class type, defines a structure consisting of fields, methods, and properties. Instances of a class type are called objects. The fields, methods, and properties of a class are called its components or members.

  • A field is essentially a variable that is part of an object. Like the fields of a record, a class' fields represent data items that exist in each instance of the class.
  • A method is a procedure or function associated with a class. Most methods operate on objectsthat is, instances of a class. Some methods (called class methods) operate on class types themselves.
  • A property is an interface to data associated with an object (often stored in a field). Properties have access specifiers, which determine how their data is read and modified. From other parts of a programoutside of the object itselfa property appears in most respects like a field.

Objects are dynamically allocated blocks of memory whose structure is determined by their class type. Each object has a unique copy of every field defined in the class, but all instances of a class share the same methods. Objects are created and destroyed by special methods called constructors and destructors.

A variable of a class type is actually a pointer that references an object. Hence more than one variable can refer to the same object. Like other pointers, class-type variables can hold the value nil. But you don't have to explicitly dereference a class-type variable to access the object it points to. For example, SomeObject.Size := 100 assigns the value 100 to the Size property of the object referenced by

SomeObject; you would not write this as SomeObjectA.Size := 100.

A class type must be declared and given a name before it can be instantiated. (You cannot define a class type within a variable declaration.) Declare classes only in the outermost scope of a program or unit, not in a procedure or function declaration.

A class type declaration has the form type className = class (ancestorClass)

memberList end;

where className is any valid identifier, (ancestorClass) is optional, and memberList declares members - that is, fields, methods, and properties - of the class. If you omit (ancestorClass), then the new class inherits directly from the predefined TObject class. If you include (ancestorClass) and memberList is empty, you can omit end. A class type declaration can also include a list of interfaces implemented by the class; see Implementing Interfaces.

Delphi for .NET supports the additional features of sealed classes and abstract classes. A sealed class is one that cannot be extended through inheritance. This includes all .NET languages that might use the sealed class. Delphi for .NET also allows an entire class to be declared as abstract, even though it does not contain any abstract virtual methods. The class declaration syntax for Delphi for .NET is:

type className = class [abstract | sealed] (ancestorType) memberList end;

Methods appear in a class declaration as function or procedure headings, with no body. Defining declarations for each method occur elsewhere in the program.

For example, here is the declaration of the TMemoryStream class from the Classes unit.

type TMemoryStream = class(TCustomMemoryStream) private

FCapacity: Longint;

procedure SetCapacity(NewCapacity: Longint); protected function Realloc(var NewCapacity: Longint): Pointer; virtual; property Capacity: Longint read FCapacity write SetCapacity; public destructor Destroy; override; procedure Clear;

procedure LoadFromStream(Stream: TStream); procedure LoadFromFile(const FileName: string); procedure SetSize(NewSize: Longint); override; function Write(const Buffer; Count: Longint): Longint;

override;

end;

TMemoryStream descends from TCustomMemoryStream (in the Classes unit), inheriting most of its members. But it defines - or redefines - several methods and properties, including its destructor method, Destroy. Its constructor, Create, is inherited without change from TObject, and so is not redeclared. Each member is declared as private, protected, or public (this class has no published members). These terms are explained below.

Given this declaration, you can create an instance of TMemoryStream as follows:

var stream: TMemoryStream;

stream := TMemoryStream.Create;

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment