根据Delphi文档,我可以使用as
运算符将TInterfacedObject
转换为接口。
但是它对我不起作用。转换会导致编译错误:"运算符不适用于此操作数类型"。
我正在使用Delphi 2007。
这里是一些代码(控制台应用程序)。标有错误的行已经标记。
program Project6;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
IMyInterface = interface
procedure Foo;
end;
TMyInterfacedObject = class(TInterfacedObject, IMyInterface)
public
procedure Foo;
end;
procedure TMyInterfacedObject.Foo;
begin
;
end;
var
o: TInterfacedObject;
i: IMyInterface;
begin
try
o := TMyInterfacedObject.Create;
i := o as IMyInterface; // <--- [DCC Error] Project6.dpr(30): E2015 Operator not applicable to this operand type
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.