5
德尔福2010年,我已经定义了一个通用的TInterfaceList如下:是否可以使用Delphi泛型TInterfaceList?
type
TInterfaceList<I: IInterface> = class(TInterfaceList)
function GetI(index: Integer): I;
procedure PutI(index: Integer; const Item: I);
property Items[index: Integer]: I read GetI write PutI; default;
end;
implementation
function TInterfaceList<I>.GetI(index: Integer): I;
begin
result := I(inherited Get(Index));
end;
procedure TInterfaceList<I>.PutI(index: Integer; const Item: I);
begin
inherited Add(Item);
end;
我已经没有任何问题,然而,却是有什么内在的风险这样做呢?是否可以添加一个枚举器来允许for循环在它上面工作?如果没有问题,我想知道为什么RTL中没有定义类似的东西。