delphi如何建立自定义类

 我来答
Vorlons
推荐于2016-05-25 · TA获得超过199个赞
知道小有建树答主
回答量:390
采纳率:0%
帮助的人:313万
展开全部
这个话题有点大。贴个Delphi的帮助信息给你吧。
如果你不能完全理解,说明你对 Delphi 的语法以及面向对象编程方面的知识还需要进一步积累。加油

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;
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式