加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Delphi中扩展Generic Collection?

发布时间:2020-12-15 09:23:22 所属栏目:大数据 来源:网络整理
导读:我有一个像这样的Generic集合: TFoo = class;TFooCollectionT: TFoo = class(TObjectDictionarystring,T) procedure DoSomething;end; 它工作正常. 现在我需要像这样扩展TFooCollection: TBar = class( TFoo ); TBarCollectionT: TBar = class(TFooCollect
我有一个像这样的Generic集合:

TFoo = class;

TFooCollection<T: TFoo> = class(TObjectDictionary<string,T>)
   procedure DoSomething;
end;

它工作正常.

现在我需要像这样扩展TFooCollection:

TBar = class( TFoo );   

TBarCollection<T: TBar> = class(TFooCollection)
   procedure DoSomethingElse;
end;

并且编译器抱怨没有定义TFooCollection.
由于TBar继承自TFoo,我想利用TFooCollection方法(可以使用TFoo和TBar项目)并使用TBar Collections做其他事情.

德尔福有可能吗?

解决方法

您知道如何扩展泛型集合TObjectDictionary,因此在扩展泛型集合TFooCollection时只需应用相同的技术. TObjectDictionary本身不指定类型 – 您需要为其两个泛型类型参数提供值.一个是硬编码为字符串的,另一个是通过转发TFooCollection中接收的泛型类型参数提供的.

同样,在为TBarCollection指定基本类型时,您可以为TFooCollection类型参数提供硬编码值,也可以从TBarCollection转发参数.你可能想做后者:

type
  TBarCollection<T: TBar> = class(TFooCollection<T>)
  end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读