Name

Class Keyword

Syntax

type Name = class
  Declarations...
  class function ...;
  class procedure...;
end;

type Name = class(BaseClass)
  ...
end;

type Name = class(BaseClass);

type ForwardDeclaredName = class;

type Name = class(BaseClass, Interface name...)
   Declarations...
end;

type Name = packed class...

type MetaClass = class of Class type;

Description

The class keyword introduces a class declaration, and it starts the declaration of a class method. If a semicolon appears immediately after the class keyword, the declaration is a forward declaration: it tells the compiler that the type name is a class type, but provides no other information about the class. You must have a complete class declaration later in the same type declaration block.

The last example above shows the declaration of a metaclass type. A variable of metaclass type can store a class reference. You can use this variable in any expression that calls for a class reference, such as calling a constructor or class method, or as the right-hand argument to an is operator. (The as operator, on the other hand, requires a static class name, not a variable class reference because it performs a type cast, and the compiler must know the target type.)

A class declaration must have a single base class and can have any number of interfaces. If you omit the base class, Delphi uses TObject. If you want to list any interfaces, you must supply the name of a base class, even if that name is TObject.

A class declaration contains zero or more sections, where each section has a particular access level. The default access level is public unless a class has RTTI (by using the $M or $TypeInfo compiler directives or inheriting from a class that has RTTI), in which case the default is published.

Each section starts with zero or more field declarations. After all the field declarations come method and property declarations. You can have any number of sections, and you can repeat sections with the same access level. Sections can appear in any order.

If you supply a base class, and a semicolon appears immediately after the closing parenthesis, you can omit the end keyword.

By default, each field is aligned, but you can use the packed directive so fields start on byte boundaries. See the packed keyword for details. If you want every class and record to be packed, use the $A or $Align compiler directive.

Class Methods

A class method is similar to an ordinary method with the following differences:

  • A class method declaration begins with the class keyword. You must use the class keyword in the class declaration and in the method’s definition.

  • You must call an ordinary method by invoking it from an object reference. You can call a class method by invoking it from an object reference or a class reference.

  • Inside a class method, Self refers to the class and does not refer to an object. This means the method cannot use any fields because there is no object to store those fields.

Tips and Tricks

A class can implement any number of interfaces. Delphi matches interface method names with method names in the class. You can redirect interface methods to different methods in the class declaration with a method resolution clause, which has the following form:

procedure Interface.InterfaceMethod = MethodName;
function Interface.InterfaceMethod = MethodName;

The MethodName is the name of a method in the containing class declaration. Redirection is especially important when a class implements multiple interfaces, and two or more interfaces have methods with the same name and arguments.

Examples

type
  TSimpleStream = class;
  TSimpleClass = class       // Implicitly inherits from TObject.
  private                    // Sections can be in any order.  A 
    fStream: TSimpleStream;  // common convention is to list sections
  public                     // in increasing order by access level.
    constructor Create(const FileName: string = ''),
    destructor Destroy; override;
    property Stream: TSimpleStream read fStream;
  end;

  TSimpleStream = class(TPersistent, ISequentialStream)
  private
    fHandle: THandle;
    fReadOnly: Boolean;
  protected
    // method of ISequentialStream
    function Read(Data: Pointer; Count: LongInt; BytesRead: PLongInt):
      HResult; stdcall;
    function Write(Data: Pointer; Count: LongInt;
      BytesWritten: PLongint): HResult; stdcall;
  public
    constructor Create(const FileName: string); overload;
    constructor Create(Handle: THandle); overload;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
  published
    property ReadOnly: Boolean read fReadOnly write fReadOnly;
  end;

  // In Delphi's Open Tools API, the IOTAWizard and IOTAFileSystem
  // interfaces both have a method named GetIDString. The TVfsWizard
  // class implements both interfaces, and uses method resolution
  // clauses to select which method implements the interface methods.
  TVfsWizard = class(TInterfacedObject, IOTAWizard, IOTAFileSystem)
  private
  // methods of IOTAFileSystem
    function DeleteFile(const FileName: string): Boolean;
    function FileAge(const FileName: string): LongInt;
    function FileExists(const FileName: string): Boolean;
    function GetBackupFileName(const FileName: string): string;
    function GetFileStream(const FileName: string; Mode: Integer):
        IStream;
    function GetFileSystemIDString: string;
    function IOTAFileSystem.GetIDString = GetFileSystemIDString;
    function GetTempFileName(const FileName: string): string;
    function IsFileBased: Boolean;
    function IsReadonly(const FileName: string): Boolean;
    function RenameFile(const OldName, NewName: string): Boolean;

  // methods of IOTAWizard
    function GetWizardIDString: string;
    function IOTAWizard.GetIDString = GetWizardIDString;
    function GetName: string;
    function GetState: TWizardState;
    procedure Execute;

  // The following function is a convenience function that anyone
  // can call to get a reference to the global IOTAServices interface.
    class function Services: IOTAServices;
  end;

See Also

Automated Directive, Constructor Keyword, Destructor Keyword, Function Keyword, Interface Keyword, Is Keyword, Packed Keyword, Private Directive, Procedure Keyword, Property Keyword, Protected Directive, Public Directive, Published Directive, Self Variable, Type Keyword, $A Compiler Directive, $Align Compiler Directive, $M Compiler Directive, $TypeInfo Compiler Directive
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset