unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type IMyInTerFace = interface(Iinterface) procedure P1;
procedure p2;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
protected
FMInTerFace: IMyInTerFace;
public { Public declarations }
end;
type TmpInTerFace = class(TInterfacedObject, IMyInTerFace)
constructor create;
procedure P1;
procedure p2;
end;
type TImplements = class(TInterfacedObject, IMyInTerFace)
protected
FMInTerFace: IMyInTerFace;
property MyInTerFace: IMyInTerFace read FMInTerFace implements IMyInTerFace;
end;var
Form1: TForm1;implementation{$R *.dfm}{ ImpInTerFace }constructor TmpInTerFace.create;beginend;procedure TmpInTerFace.P1;
begin
ShowMessage('P1');
end;procedure TmpInTerFace.p2;
begin
ShowMessage('P2');
end;procedure TForm1.Button1Click(Sender: TObject);
var
TImp:TImplements; //实现委托属性类
InTet:IMyInTerFace; //接口
TnTerImp:TmpInTerFace; //实现接口类的对象
begin
TImp:=TImplements.Create;
Timp.FMInTerFace:=TnTerImp.create;
InTet:=TImp;
InTet.P1;
end;end.出现错误 [Warning] Unit1.pas(68): Variable 'TnTerImp' might not have been initialized应该怎么去解决这个问题呢?
改成
Timp.FMInTerFace:=TmpInTerFace.create;
TnTerImp -- 是变量,不是类
var
TImp:TImplements; //实现委托属性类
InTet:IMyInTerFace; //接口
TnTerImp:TmpInTerFace; //实现接口类的对象
begin
TImp:=TImplements.Create;
Timp.FMInTerFace:= TmpInTerFace.create; //TnTerImp.create; 變理與類搞混了吧
InTet:=TImp;
InTet.P1;
end;