Within a class declaration, methods appear as procedure and function headings, which work like forward declarations. Somewhere after the class declaration, but within the same module, each method must be implemented by a defining declaration. For example, suppose the declaration of TMyClass includes a method called DoSomething:
type
TMyClass = class(TObject)
...
procedure DoSomething;
...
end;
A defining declaration for DoSomething must occur later in the module:
procedure
TMyClass.DoSomething;
begin
...
end;
While a class can be declared in
either the interface or the implementation section of a unit, defining
declarations for a class’s methods must be in the implementation section.
In the heading of a defining
declaration, the method name is always qualified with the name of the class to
which it belongs. The heading can repeat the parameter list from the class
declaration; if it does so, the order, type, and names of the parameters must
match exactly, and, if the method is a function, so must the return value.
Method declarations can include
special directives that are not used with other functions or procedures.
Directives should appear in the class declaration only, not in the defining
declaration, and should always be listed in the following order:
reintroduce; overload; binding; calling convention;
abstract; warning
where binding is virtual, dynamic,
or override; calling convention is register, pascal,
cdecl, stdcall, or safecall; and warning is platform,
deprecated, or library.
方法作为过程和函数的首部出现在类的声明中,类似于向前(forward)声明。在相同模块中、类声明之后的某处,每个方法必需被相应的定义声明实现。例如,假设TMyClass类的声明中包括一个叫做DoSomething的方法:
type
TMyClass = class(TObject)
...
procedure DoSomething;
...
end;
那么,DoSomething的定义声明必需随后(不必紧随其后)出现在同一模块中:
procedure
TMyClass.DoSomething;
begin
...
end;
类既可以声明在单元的接口节,也可以声明在单元的实现节,而类方法的定义声明必需在单元的实现节中。
在定义声明首部中,方法名总是被其所属的类限定。方法首部中可以重复类声明中的参数列表;如果是这样,那么参数的顺序、类型和名字必需严格匹配,并且,如果方法是一个函数,那么返回值也必需严格匹配。
方法声明可以包括特别的指示字,这些指示字不能用于其他的函数或过程。指示字只能出现在类声明中,不能出现在定义声明中,并且必需总是按照下列顺序列出:
reintroduce; overload; binding; calling convention;
abstract; warning
这里的绑定(binding)是指指示字virtual、dynamic或override;调用约定(calling convention)是指指示字register、pascal、cdecl、stdcall或safecall;警告(warning)是指指示字platform、deprecated、或library。