20120726
Delphi7+Win2003
1) 创建一个接口:IInterfaceTest
2) 实现IInterfaceTest接口:TInterfaceTest
3) 创建一个窗体TfrmInterfaceTest,并在窗体中一定一个IInterfaceTest的属性
4) 外部打开TfrmInterfaceTest,后关闭,提示错误
IInterfaceTest=interface procedure Test(); end; |
TInterfaceTest=class(TInterfacedObject,IInterfaceTest) procedure Test(); end;
procedure TInterfaceTest.Test; begin ShowMessage('TEST'); end; |
TfrmInterfaceTest = class(TForm) btnInterfaceTest: TButton; procedure btnInterfaceTestClick(Sender: TObject); private FInterfaceTestPart: IInterfaceTest; procedure SetInterfaceTestPart(const Value: IInterfaceTest); //destructor Destroy; override; { Private declarations } public { Public declarations } property InterfaceTestPart:IInterfaceTest read FInterfaceTestPart write SetInterfaceTestPart; end;
procedure TfrmInterfaceTest.btnInterfaceTestClick(Sender: TObject); begin FInterfaceTestPart.Test; end;
procedure TfrmInterfaceTest.SetInterfaceTestPart( const Value: IInterfaceTest); begin FInterfaceTestPart := Value; end; |
procedure TfrmMain.btnInterfaceTestClick(Sender: TObject); var LFrm:TfrmInterfaceTest; LInterfaceTest:TInterfaceTest; begin LFrm:=TfrmInterfaceTest.Create(nil); LInterfaceTest:=TInterfaceTest.Create; LFrm.InterfaceTestPart:=LInterfaceTest; LFrm.ShowModal; LInterfaceTest.Free; LFrm.Free; end; |
关闭frmInterfaceTest窗口的时候,发生异常
1 unit frmInterfaceTestUnit; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls; 8 9 type 10 IInterfaceTest=interface 11 procedure Test(); 12 end; 13 14 TInterfaceTest=class(TInterfacedObject,IInterfaceTest) 15 procedure Test(); 16 end; 17 18 TfrmInterfaceTest = class(TForm) 19 btnInterfaceTest: TButton; 20 procedure btnInterfaceTestClick(Sender: TObject); 21 private 22 FInterfaceTestPart: IInterfaceTest; 23 procedure SetInterfaceTestPart(const Value: IInterfaceTest); 24 { Private declarations } 25 public 26 { Public declarations } 27 property InterfaceTestPart:IInterfaceTest read FInterfaceTestPart write SetInterfaceTestPart; 28 end; 29 30 var 31 frmInterfaceTest: TfrmInterfaceTest; 32 33 implementation 34 35 {$R *.dfm} 36 37 { TInterfaceTest } 38 39 procedure TInterfaceTest.Test; 40 begin 41 ShowMessage('TEST'); 42 end; 43 44 procedure TfrmInterfaceTest.btnInterfaceTestClick(Sender: TObject); 45 begin 46 FInterfaceTestPart.Test; 47 end; 48 49 procedure TfrmInterfaceTest.SetInterfaceTestPart( 50 const Value: IInterfaceTest); 51 begin 52 FInterfaceTestPart := Value; 53 end; 54 55 end. 56 57 unit Main; 58 59 interface 60 61 uses 62 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 63 Dialogs, StdCtrls,frmInterfaceTestUnit; 64 65 type 66 TfrmMain = class(TForm) 67 btnInterfaceTest: TButton; 68 procedure btnInterfaceTestClick(Sender: TObject); 69 private 70 { Private declarations } 71 public 72 { Public declarations } 73 end; 74 75 var 76 frmMain: TfrmMain; 77 78 implementation 79 80 {$R *.dfm} 81 82 procedure TfrmMain.btnInterfaceTestClick(Sender: TObject); 83 var 84 LFrm:TfrmInterfaceTest; 85 LInterfaceTest:TInterfaceTest; 86 begin 87 LFrm:=TfrmInterfaceTest.Create(nil); 88 LInterfaceTest:=TInterfaceTest.Create; 89 LFrm.InterfaceTestPart:=LInterfaceTest; 90 LFrm.ShowModal; 91 LInterfaceTest.Free; 92 LFrm.Free; 93 end; 94 95 96 end.
还有用delphi的啊!
procedure TfrmMain.btnInterfaceTestClick(Sender: TObject);
var
LFrm:TfrmInterfaceTest;
LInterfaceTest:TInterfaceTest;
begin
LFrm:=TfrmInterfaceTest.Create(nil);
LInterfaceTest:=TInterfaceTest.Create;
LFrm.InterfaceTestPart:=LInterfaceTest;
LFrm.ShowModal;
//因为你的LFrm是窗体,在showModal关闭后就自动释放掉了,所以下面的
// LInterfaceTest.Free; //已经被上面的LFrm释放掉了,这个必须去掉
// LFrm.Free; //这个保留是不会报错的,但是也可以不要
end;