1
type IMyInTerFace = interface(Iinterface) // 接口类型
2
3
procedure P1;
4
procedure p2;
5
end;
6
type TmpInTerFace = class(TInterfacedObject, IMyInTerFace) //实现接口的类
7
constructor create;
8
procedure P1;
9
procedure p2;
10
end;
11
type TImplements = class(TInterfacedObject, IMyInTerFace) //委托属性的类
12
protected
13
FMInTerFace: IMyInTerFace;
14
property MyInTerFace: IMyInTerFace read FMInTerFace implements IMyInTerFace;
15
end;
16
17
procedure TmpInTerFace.P1;
18
begin
19
ShowMessage('P1');
20
end;
21
22
procedure TmpInTerFace.p2;
23
begin
24
ShowMessage('P2');
25
end;
26
27
28
var
29
TImp:TImplements; //实现委托属性类
30
InTet:IMyInTerFace; //接口 {接口没有构造函数和析构函数,它们不能被(直接)实例化,除非使用实现了它们(的方法)的类}
31
TnTerImp:TmpInTerFace; //实现接口类的对象
32
begin
33
TImp:=TImplements.Create;
34
TnTerImp:=TmpInTerFace.create; // 变量初始化.注意只有这样才能赋值.
35
Timp.FMInTerFace:=TnTerImp; //也可以直接忽略TnTerImp变量而直接使用Timp.FMInTerFace:=TmpInTerFace.Create.
36
InTet:=TImp;
37
InTet.P1;
38
end;
39
40
end.
41