Originale-mail to me for new edition

 

About class types

 

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.

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 TStream (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); for explanations of these terms, see Visibility of class members.

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

var stream: TMemoryStream;

stream := TMemoryStream.Create;

 

Inheritance and scope

Forward declarations and mutually dependent classes

 

Topic groups

 

See also

Classes and objects: Overview

Constructors

Data types and variables: Overview

Destructors

Object types

 

 

译文

 

关于类类型

 

类类型在其被例示之前必需被声明并且给出一个名字。(不能在变量声明中定义类类型。)只能在程序过单元的最外层的作用域声明类,不能在过程或函数的声明中声明类。

类类型声明具有如下形式

type className = class (ancestorClass)

  memberList

end;

这里的className是任意有效的标识符,保留字class之后的 (ancestorClass) 是可选的,memberList声明了类成员,即域、方法、属性等。如果忽略了 (ancestorClass) ,那么新的类直接从预定义的TObject类继承。如果含有 (ancestorClass) 并且memberList是空的,那么可以忽略最后的保留字end。类类型的声明还可以包括由类实现的接口列表,见实现接口

方法作为函数或过程首部出现在类声明中,没有例程主体。每个方法的定义声明出现在程序的其他地方。

例如,下面是Classes单元中TMemoryStream类的声明,

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遗传自TStream(在Classes单元),继承了其大多数成员。但它定义或重定义了几个方法和属性,包括其析构器方法Destroy。其构造器Create继承自TObject,没有任何改变,因此没有再声明。每个成员成为为私有的(private)、保护的(protected)或公共的(public),该类中没有公布的(published)成员;这些术语的说明见类成员的可见度

对于上面给出的声明,可以如下创建TMemoryStream的实例:

var stream: TMemoryStream;

stream := TMemoryStream.Create;

 

继承和作用域

向前声明和相互依赖的类

 

主题组

 

相关主题

类和对象:概述

构造器

数据类型和变量:概述

析构器

对象类型

 

 

编者注

要理解类和一般数据类型的区别,需要参考有关面向对象技术的资料。从Object Pascal实现和管理等方面区别,一般数据类型(简单类型、结构类型等)在变量标识符被声明时就自动获得内存;指针类型(包括类型指针)需要通过标准例程GetMemNew分配内存、FreeMemDispose释放内存;类类型则需要通过其构造器(一般是Create)完成分配内存及其他初始化工作,通过析构器(一般用DestroyFree)完成释放内存及其他结束工作。此外,类类型和一般数据类型最大的区别在于,类类型使得面向对象技术在应用开发中得到充分利用。例如,VCL中提供的绝大多数类类型都继承自TObject类(或其后裔),而TObject中封装了一些相当重要的有关内存管理的成员,这为开发者创建或扩展新的类类型提供了简捷的途径。充分利用已有资源,开发者才能更好地实现快速应用开发。