这看起来相当简单,也许我只是缺少一些语法胶水......这是我简单的通用(Delphi XE3)示例:
unit Unit1;
interface
uses
generics.collections;
type
X = class
public
Id: Integer;
end;
XList<T : X> = class( TObjectList<T> )
function Find(Id: Integer) : T;
end;
Y = class(X)
end;
YList = class(XList<Y>)
end;
implementation
{ XList<T> }
function XList<T>.Find(Id: Integer): T;
var
t: X;
begin
for t in Self do
if t.Id = Id then
Result := t;
end;
end.
这不会与“[dcc32 Error] Unit1.pas(41): E2010 Incompatible types: 'Y' and 'X'”一起编译。归根结底:
YList = class(XList<Y>)
end;
Y 派生自 X 那么为什么会有问题呢?