Mar*_*ima 4 delphi generics collections
我有一个像这样的Generic集合:
TFoo = class;
TFooCollection<T: TFoo> = class(TObjectDictionary<string, T>)
procedure DoSomething;
end;
Run Code Online (Sandbox Code Playgroud)
它工作正常.
现在我需要像这样扩展TFooCollection:
TBar = class( TFoo );
TBarCollection<T: TBar> = class(TFooCollection)
procedure DoSomethingElse;
end;
Run Code Online (Sandbox Code Playgroud)
并且编译器抱怨没有定义TFooCollection.由于TBar继承自TFoo,我想利用TFooCollection方法(可以使用TFoo和TBar项目)并使用TBar Collections做其他事情.
德尔福有可能吗?
您知道如何扩展泛型集合TObjectDictionary,因此在扩展泛型集合时只需应用相同的技术即可TFooCollection.TObjectDictionary不会单独指定类型 - 您需要为其两个泛型类型参数提供值.一个你硬编码的string,另一个你通过转发收到的泛型类型参数提供的TFooCollection.
同样,在指定基本类型时TBarCollection,您可以为TFooCollectiontype参数提供硬编码值,也可以从中转发参数TBarCollection.你可能想做后者:
type
TBarCollection<T: TBar> = class(TFooCollection<T>)
end;
Run Code Online (Sandbox Code Playgroud)