【发布时间】:2011-06-14 04:43:59
【问题描述】:
下面的例子给了我这个错误:[DCC Error] Unit2.pas(54): E2010 Incompatible types: 'IBar' and 'Unit2.TFoo<Unit2.IBar>'
我认为问题出在 Self.Create 周围 因为经过多次尝试编译后,我不小心输入了 FFoo := TBar(Self).Create;它编译并工作。
我正在使用 Delphi XE
IFoo = interface
end;
TFoo<T: IInterface> = class(TInterfacedObject, IFoo)
private class var
FFoo: T;
public class
function Instance: T;
end;
IBar = interface(IFoo)
end;
TBar = class(TFoo<IBar>, IBar)
end;
class function TFoo<T>.Instance: T;
begin
if not Assigned(FFoo) then
begin
FFoo := Self.Create;
end;
Result := FFoo;
end;
【问题讨论】:
-
错误告诉你一个行号。也许您可以准确指出那是哪一行,而不是猜测问题可能出在哪里?
-
错误就在“end”之后的那一行
-
演员
TBar(Self).Create是不需要的,因为您不确定 Self 是 TBar 并且它将打破泛型的概念(不能用于其他类)!跨度> -
这里不支持行号真的很可惜。
-
2017 和 Tokyo.2 还是一样