1.類的聲明格式
type
className = class [abstract | sealed] (ancestorClass)
memberList
end;
2.類的聲明和使用
//定義
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;
//使用
var stream: TMemoryStream;
stream := TMemoryStream.Create;
3.類的繼承
//繼承一個類
type TSomeControl = class(TControl);
//根類 TObject
type TMyClass = class
...
end;
//等價於
type TMyClass = class(TObject)
...
end;
4.基類和子類
type
TFigure = class(TObject);
TRectangle = class(TFigure);
TSquare = class(TRectangle);
var
Fig: TFigure;
//the variable Fig can be assigned values of type TFigure, TRectangle, and TSquare.
5.獲取對象類型
type objectTypeName = object (ancestorObjectType)
memberList
end;
6.類的關聯
type
TFigure = class; // forward declaration
TDrawing = class
Figure: TFigure;
...
end;
TFigure = class // defining declaration
Drawing: TDrawing;
...
end;