程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi語言學習7-類和對象

Delphi語言學習7-類和對象

編輯:Delphi

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;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved