Delphi 泛型应用
2015-09-21 16:19
411 查看
TArray TEnumerator(抽象) TEnumerable(抽象)
实际使用:TList TQueue TStack TPair TDictionary ,内部都包含 TValueEnumerator
碰到对象:TObjectList TObjectQueue TObjectStack TObjectDictionary
另外haiy:TThreadedQueue
比如以前Delphi没有integer list,用泛型,我们可以这样声明:
var
list: TList<Integer>;
i:integer;
begin
list := TList<Integer>.Create;
list.Add(1);
list.Add(2);
list.Add(3);
list.Items[2] := 8;
for I := 0 to List.Count-1 do
ShowMessage(IntToStr(List.Items[i]));
list.Free;
end;
TDictionary类是一个name,value容器,内部是哈希索引,所以对于数据查找非常高效,如下面的代码:
var
Dict: TDictionary<string, string>;
begin
Dict := TDictionary<string, string>.Create;
Dict.Add('key1', 'value1');
Dict.Add('key2', 'value2');
if not Dict.ContainsKey('key3') then
Dict.Add('key3','value3');
Dict.Free;
end;
上面的代码中name和value都是string类型,实际应用中可以用其它的类型比如integer,double之类
因为Object Pascal没有垃圾收集,所以对应的每种泛型类都有一个对应引用类型实现,比如TObjectList,TObjectDictionary,这些类实现中, 当删除元素时会自动帮你释放对象。
上面的TList和TDictionary在Generics.Collections单元,有兴趣可以研究一下代码。除了已有的泛型类之外,你可以编写自己的泛型类,感兴趣的朋友可以google相关文章。
几本参考书:
http://www.cnblogs.com/del/archive/2008/08/15/1268786.html http://read.pudn.com/downloads161/ebook/731442/delphi-generics.pdf http://www.jcolibri.com/articles/prog_objet_composants/delphi_generics/delphi_generics.html http://www.danysoft.com/free/using-new-delphi-coding-styles-and-architectures-marco-cantu.pdf http://docwiki.embarcadero.com/CodeExamples/XE5/en/Generics_Collections_TList_(Delphi) http://www.felix-colibri.com/papers/oop_components/delphi_generics_tutorial/delphi_generics_tutorial.html
实际使用:TList TQueue TStack TPair TDictionary ,内部都包含 TValueEnumerator
碰到对象:TObjectList TObjectQueue TObjectStack TObjectDictionary
另外haiy:TThreadedQueue
比如以前Delphi没有integer list,用泛型,我们可以这样声明:
var
list: TList<Integer>;
i:integer;
begin
list := TList<Integer>.Create;
list.Add(1);
list.Add(2);
list.Add(3);
list.Items[2] := 8;
for I := 0 to List.Count-1 do
ShowMessage(IntToStr(List.Items[i]));
list.Free;
end;
TDictionary类是一个name,value容器,内部是哈希索引,所以对于数据查找非常高效,如下面的代码:
var
Dict: TDictionary<string, string>;
begin
Dict := TDictionary<string, string>.Create;
Dict.Add('key1', 'value1');
Dict.Add('key2', 'value2');
if not Dict.ContainsKey('key3') then
Dict.Add('key3','value3');
Dict.Free;
end;
上面的代码中name和value都是string类型,实际应用中可以用其它的类型比如integer,double之类
因为Object Pascal没有垃圾收集,所以对应的每种泛型类都有一个对应引用类型实现,比如TObjectList,TObjectDictionary,这些类实现中, 当删除元素时会自动帮你释放对象。
上面的TList和TDictionary在Generics.Collections单元,有兴趣可以研究一下代码。除了已有的泛型类之外,你可以编写自己的泛型类,感兴趣的朋友可以google相关文章。
几本参考书:
http://www.cnblogs.com/del/archive/2008/08/15/1268786.html http://read.pudn.com/downloads161/ebook/731442/delphi-generics.pdf http://www.jcolibri.com/articles/prog_objet_composants/delphi_generics/delphi_generics.html http://www.danysoft.com/free/using-new-delphi-coding-styles-and-architectures-marco-cantu.pdf http://docwiki.embarcadero.com/CodeExamples/XE5/en/Generics_Collections_TList_(Delphi) http://www.felix-colibri.com/papers/oop_components/delphi_generics_tutorial/delphi_generics_tutorial.html
相关文章推荐
- Delphi XE新语法/新功能
- Delphi新语法 For ..In
- Delphi 关键 重启 注销
- Delphi图像处理控件
- Delphi 完整的Bug决议工具EurekaLog的使用
- Delphi XE7的蓝牙 Bluetooth
- delphi的public 与 published
- Delphi中那些容易混淆的基础
- Delphi 调试
- Delphi调用WebService(通过SoapHeader认证)经验总结
- delphi:with do用法
- Delphi开发OCX详细步骤总结
- Delphi COM编程学习笔记(1)
- delphi引用类的顺序
- Delphi 移位的问题
- Delphi星期几的判断
- delphi 2007 远程调试
- Delphi调用网页美化SQL
- Delphi中Chrome Chromium、Cef3学习笔记(五)
- Delphi中的四舍五入函数