Interfaces,
like classes, can be declared only in the outermost scope of a program or unit,
not in a procedure or function declaration. An interface type declaration has
the form
type interfaceName
= interface (ancestorInterface)
['{GUID}']
memberList
end;
where (ancestorInterface) and ['{GUID}'] are optional. In most respects, interface declarations resemble class declarations, but the following restrictions apply.
· The memberList can include only methods and properties. Fields are not allowed in interfaces.
· Since an interface has no fields, property read and write specifiers must be methods.
· All members of an interface are public. Visibility specifiers and storage specifiers are not allowed. (But an array property can be declared as default.)
· Interfaces have no constructors or destructors. They cannot be instantiated, except through classes that implement their methods.
· Methods cannot be declared as virtual, dynamic, abstract,
or override. Since interfaces do not implement their own methods, these
designations have no meaning.
Here is
an example of an interface declaration:
type
IMalloc = interface(IInterface)
['{00000002-0000-0000-C000-000000000046}']
function Alloc(Size:
Integer): Pointer; stdcall;
function Realloc(P:
Pointer; Size: Integer): Pointer; stdcall;
procedure Free(P:
Pointer); stdcall;
function GetSize(P:
Pointer): Integer; stdcall;
function DidAlloc(P:
Pointer): Integer; stdcall;
procedure
HeapMinimize; stdcall;
end;
In some interface declarations, the
interface reserved word is replaced by dispinterface. This
construction (along with the dispid, readonly, and writeonly
directives) is platform-specific and is not used in Linux programming.
Calling conventions for
interfaces
Dispatch interface
types (Windows only)
与类相似,接口只能声明在程序或单元的最外层的作用域中,不能在过程或函数声明中。接口声明具有如下形式:
type interfaceName
= interface (ancestorInterface)
['{GUID}']
memberList
end;
这里的 (ancestorInterface) 和 ['{GUID}'] 都是可选的。接口声明在大多数方面与类声明相似,但接口声明受到下列限制:
· 成员列表memberList 只能包括方法和属性。接口中不允许含有域。
· 因为接口中没有域,所以属性的read和write说明符都必须是方法。
· 接口中所有的成员都是公共的。可见度说明符和存储说明符都是不允许的。(但数组属性可以被声明为default。)
· 接口没有构造器和析构器。除了可以通过实现接口的类之外,接口不能被实例化。
· M接口中的方法不能声明为虚拟的(virtual)、动态的(dynamic)、抽象的(abstract)或覆盖的(override)。因为接口不能实现它们自己的方法,所以这些指示没有意义。
下面是接口声明的例子:
type
IMalloc = interface(IInterface)
['{00000002-0000-0000-C000-000000000046}']
function Alloc(Size:
Integer): Pointer; stdcall;
function Realloc(P:
Pointer; Size: Integer): Pointer; stdcall;
procedure Free(P:
Pointer); stdcall;
function GetSize(P:
Pointer): Integer; stdcall;
function DidAlloc(P:
Pointer): Integer; stdcall;
procedure
HeapMinimize; stdcall;
end;
在某些接口声明中,保留字interface被dispinterface代替。这一结构(与dispid、readonly和writeonly一道)是平台特有的并且不能用于Linux编程。