type
IMyInterface = interface //接口类型
procedure P1;
procedure P2;
end;
TMyImplClass = class //类类型
procedure P1;
procedure P2;
end;
TMyClass = class(TInterfacedObject, IMyInterface)
FMyImplClass:
TMyImplClass; //类类型变量
property MyImplClass:
TMyImplClass read
FMyImplClass implements
IMyInterface;
procedure IMyInterface.P1 = MyP1; //方法别名
procedure MyP1;
end;
implementation
{$R *.dfm}
{ TMyImplClass }
procedure TMyImplClass.P1;
begin
ShowMessage('TMyImplClass.P1');
end;
procedure TMyImplClass.P2;
begin
ShowMessage('TMyImplClass.P2');
end;
{ TMyClass }
procedure TMyClass.MyP1;
begin
ShowMessage('TMyClass.MyP1');
end;
procedure TForm1.btn1Click(Sender: TObject);
var
MyClass: TMyClass;
MyInterface: IMyInterface;
begin
MyClass := TMyClass.Create;
MyClass.FMyImplClass := TMyImplClass.Create; //委托属性创建(实例化)
MyInterface := MyClass;
MyInterface.P1; // 调用 TMyClass.MyP1;
MyInterface.P2; // 调用 TImplClass.P2;
end;