Delphi接口通用函数返回TobjectList<;T>;

Delphi接口通用函数返回TobjectList<;T>;,delphi,delphi-xe4,Delphi,Delphi Xe4,为什么这不起作用?我得到一个E2511类型参数'T'必须是类类型吗 type IBaseProvider<T> = Interface function GetAll: TObjectList<T>; end; type TCar = class(TInterfacedPersistent, IBaseProvider<TVehicle>) function GetAll: TObjectList<TVehicle>;

为什么这不起作用?我得到一个E2511类型参数'T'必须是类类型吗

type
  IBaseProvider<T> = Interface
    function GetAll: TObjectList<T>;
  end;

type
  TCar = class(TInterfacedPersistent, IBaseProvider<TVehicle>)
    function GetAll: TObjectList<TVehicle>;
  end;

implementation

function TCar.GetAll: TObjectList<TVehicle>;
begin
  // do something with Objectlist
  Result := ObjectList
end;
类型
IBaseProvider=接口
函数GetAll:TObjectList;
结束;
类型
TCar=类别(TInterfacedPersistent,iBasProvider)
函数GetAll:TObjectList;
结束;
实施
函数TCar.GetAll:TObjectList;
开始
//用Objectlist做些什么
结果:=对象列表
结束;
的参数T被约束为类

type
  TObjectList<T: class> = class(TList<T>)
    ....
  end;
类型
TObjectList=类(TList)
....
结束;
您需要声明一个约束,该约束在您的类型上暗示了这一点。例如,您可以声明相同的约束:

type
  IBaseProvider<T: class> = Interface
    function GetAll: TObjectList<T>;
  end;
类型
IBaseProvider=接口
函数GetAll:TObjectList;
结束;
或者,只要满足
TObjectList
约束,就可以声明一个更强的约束。如果不想约束参数,则需要使用
TList


如果您不熟悉通用约束,文档应填补空白:

谢谢,David。但是关于词典呢。我可以用java编写:HashMap>();。如何用德尔菲写作?这是一个不同的问题。让我们先完成这个问题。