由于没有太多编写接口的经验,此处仅简单说明语法。后期对接口有更多认知和经验后再进行详细记录。
Delphi
Delphi的接口是侵入式接口,并且是单继承的,但类可以同时实现多个接口,类声明时需要显示声明实现了哪些接口。
声明
type
//直接声明
IMyInterface1 = interface
function Func1: Integer; //函数
procedure Proc1(Value: Integer); //过程
property MI: Integer read Func1 write Proc1; //属性
end;
//从已有接口继承
IMyInterface2 = interface(IMyInterface1)
procedure Proc2;
end;
//含有 GUID 的接口可以公开给其它进程调用
IMyInterface3 = interface
['{3E51374A-D0E8-4C84-AA30-9634409E45DD}']
procedure Proc3;
end;
Delphi已经提供了基接口IInterface,自己声明的接口最好从IInterface继承。
实现
type
//含接口的类的声明
TMyClass = class(基类, 接口)
public
procedure Proc; //接口方法
end;
//接口实现
procedure TMyClass.Proc;
begin
<