Z
zhenqiang
Unregistered / Unconfirmed
GUEST, unregistred user!
有那位能够清楚的解释一下在DEPHI里面接口的概念,如果有相关的例子最好不过了,小弟在这里送分了!!!
unit mtReaper;
interface
type
ImtReaper=interface
['{F3E97960-3F35-11D7-B847-001060806215}']
end;
TmtReaper=class(TInterfacedObject,ImtReaper)
private
FObject:TObject;
public
constructor Create(AObject:TObject);
destructor Destroy;override;
end;
implementation
uses SysUtils;
constructor TmtReaper.create(AObject:TObject);
begin
FObject:=AObject;
end;
destructor TmtReaper.Destroy;
begin
FreeAndNil(FObject);
Inherited;
end;
end.
unit frmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TNoisyDeath = class(TObject)
public
destructor Destroy;override;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure WaitAWhile;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses mtReaper;
{$R *.dfm}
destructor TNoisyDeath.Destroy;
begin
showMessage('对象销毁了!');
inherited;
end;
procedure TForm1.WaitAWhile;
var i:Integer;
begin
for i :=0 to 5000 do
begin
caption:=Inttostr(i);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var NoisyDeath:TNoisyDeath;
begin
NoisyDeath:=TNoisyDeath.create;
try
waitAWhile;
finally
NoisyDeath.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
NoisyDeath:TNoisyDeath;
girm:ImtReaper;
begin
NoisyDeath:=TNoisyDeath.create;
girm:=TmtReaper.Create(NoisyDeath);
waitAWhile;
end;
end.