zoukankan      html  css  js  c++  java
  • Dephi泛型

    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

  • 相关阅读:
    MSSQL错误1069解决方法
    Xamarin的Mono for Android目前可运行于MIPS上
    Oracle大数据机和连接器产品支持与Hadoop和Cloudera Manager集成
    百度1
    恒生电子
    IGT中国
    经典笔试题——a和&a有什么区别
    腾讯HTTP协议1
    腾讯http协议2
    面向对象的三大特征
  • 原文地址:https://www.cnblogs.com/findumars/p/3541193.html
Copyright © 2011-2022 走看看