以下示例给我带来了这个错误:
我正在使用Delphi XE。
[DCC Error] Unit2.pas(54): E2010 不兼容的类型:'IBar' 和 '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;
TBar(Self).Create
是不必要的,因为您不确定Self是否为TBar 并且它会破坏泛型的概念(不能用于另一个类)! - TridenT