您的位置:首页 > 编程语言 > Delphi

Delphi 接口托管实现

2015-07-31 17:37 387 查看
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

IMyInterface = interface(IUnknown)
procedure ShowString(s: string);
end;

TMyClass = class(TInterfacedObject, IMyInterface)
public
procedure ShowString(s: string);
end;

TSecondClass = class(TInterfacedObject, IMyInterface)
protected
myInterface: IMyInterface;
protected
property my: IMyInterface read myInterface implements IMyInterface;
public
constructor Create(AOwner: TObject); overload;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
myclass: TMyClass;
mySecondclass: TSecondClass;
begin
myclass := TMyClass.Create;
mySecondclass := TSecondClass.Create(nil);
myclass.ShowString('sss');
mySecondclass.my.ShowString('aaa');
myclass.Free;
mySecondclass.Free;
end;

{ TMyClass }

procedure TMyClass.ShowString(s: string);
begin
ShowMessage(s);
end;

{ TSecondClass }

constructor TSecondClass.Create(AOwner: TObject);
begin
myInterface := TMyClass.Create;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: