所在的单元

TList(Classes.pas)

TObjectList(Contnrs.pas)

TObjectList对象的创建方法有一个参数: constructor TObjectList.Create(AOwnsObjects: Boolean); 从字面就可理解其意义:拥有对象集与否。

帮助文档

If the OwnsObjects property is set to true (the default), TObjectList controls the memory of its objects, freeing an object when its index is reassigned; when it is removed from the list with the Delete, Remove, or Clear method; or when the TObjectList instance is itself destroyed.
这段话的意思就是TObjectList 的几个删除方法和释放方法都受参数AOwnsObjects的影响。 用的默认参数值,释放时ObjectList里的对象一块释放掉.

TOjectList = Class (Tlist);
TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢?
首先是 TObject 作为对象可以方便使用,无需指针强制。
丰富了 Notify 针对当前状态处理,比如如果是删除就将该点的引用一并清理掉;

procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if (Action = lnDeleted) and OwnsObjects then
TObject(Ptr).Free; inherited Notify(Ptr, Action);
end;

看notify方法会发现:如果TOjectList.OwnsObjects=True时(默认的创建),释放的时候会连里面的对象一块释放掉。  TOjectList.OwnsObjects=False时,释放的时候不会释放里面的对象。
Tlist 中,有 Clear(),将呼叫 SetCount,SetCapacity;即删除所有。

procedure TList.Clear();
begin
SetCount(0);
SetCapacity(0);
end;

当该对象销毁时,也会自动调用Clear() 清除所有。

destructor TList.Destroy;
begin
Clear();
end;

如果从 Tlist 继承也必须实现 Notify() ,方便资源释放,减少麻烦。 List和OjectList释放的区别如下例子:

 procedure TForm1.Button1Click(Sender: TObject);
var
list: TObjectList;
i: Integer;
btn: TButton;
begin
list := TObjectList.Create;
for i := to do
begin
btn := TButton.Create(Self);
with btn do begin
Caption := Format('Btn %d', [i+]);
Parent := Self;
end;
list.Add(btn);
end;
ShowMessage('TObjectList 释放时, 会同时释放其中的对象');
list.Free;
end; procedure TForm1.Button2Click(Sender: TObject);
var
list: TList;
i: Integer;
btn: TButton;
begin
list := TList.Create;
for i := to do
begin
btn := TButton.Create(Self);
with btn do
begin
Caption := Format('Btn %d', [i+]);
Parent := Self;
end;
list.Add(btn);
end;
ShowMessage('TList 释放后, 其中的对象并未释放');
list.Free;
end;

如果用TObjectList来存储,当调用sort方法时要注意先将owerobject设置为False

还有一个有意思的类,相信我们都用过TStringList,有一个AddObject方法,可以操作对象,类似TObjectList

总结:如果有一个对象需要用到列表,最好从 TOjectList 开始继承,对资源释放有完善的处理机制。

TList TObjectList的区别和使用的更多相关文章

  1. TList,TObjectList 使用——资源释放

    TOjectList = Class (Tlist); TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢? 首先是 TObject 作为对象 ...

  2. TObjectList

    AOwnsObjects = true 就是  objectlist释放的时候,里面的对象一并释放. TObjectList对象的创建方法有一个参数:constructor TObjectList.C ...

  3. Delphi容器类之---TList、TObjectList、TComponentList、TClassList

    转载自:http://blog.csdn.net/iseekcode/article/details/4922001 从Delphi5开始VCL中增加了新的Contnrs单元,单元中定义了8个新的类, ...

  4. Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析

    转载自:http://blog.csdn.net/jqandjq/article/details/5429137 看了这里标题,大家可能以为我会谈TListBox控件,那就错了.我要谈的是Delphi ...

  5. Delphi容器类之---Tlist,TStringlist,THashedStringlist的效率比较

    转载自:http://www.ylzx8.cn/windows/delphi/73200.html 本人在做一个测试,服务器是IOCP的,我假定最大链接数是50000个. 测试背景:如果每个链接之间的 ...

  6. Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>

    Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...

  7. 序列化TList of objects(摘自danieleteti的网站)

    Some weeks ago a customer asked to me if it is possibile serialize a TList of objects. “Hey, you sho ...

  8. TStack,TQueue,TObjectList,TObjectStack等等

    TStack,TQueue,TObjectList,TObjectStack等等,都在Contnrs.pas单元里,需要手动添加. 不同于TList类,TObjectList对象将销毁任何从列表中删除 ...

  9. 教程-TObjectList.Clear、TStringList.Clear方法对象有没有被释放

    相关资料: http://www.cnblogs.com/rogge7/p/4631796.html delphiTStringList通过AddObject方法添加对象. object里存的只是指向 ...

随机推荐

  1. Self-Attention 和 Transformer

    1.Self-Attention 之前的RNN输入是难以并行化的,我们下一个输入可能依赖前一个输出,只有知道了前面的输出才能计算后面的输出. 于是提出了 self-attention ,但是这时候 $ ...

  2. vue 全局filter的坑

    下面连段代码的filter放在不同的位子会有不同的效果, 1.filter放在new vue之后,居然不起作用 <script> new Vue({ el: '#app', data: { ...

  3. Emacs中的前进后退jump-tree

    Emacs中的前进后退jump-tree */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #8394 ...

  4. 让 Git Bisect 帮助你

    让 Git Bisect 帮助你 英文原文:Letting Git Bisect Help You   Git 提供来很多的工具来帮助我们改进工作流程. bisect 命令就是其中之一, 虽然由于使用 ...

  5. Python之文件路径名的操作

    使用 os.path 模块中的函数来操作路径名 import os # 获取当前文件路径 path=os.path.abspath(__file__) # 获取绝对路径 /home/zzy/Pycha ...

  6. 20180306-time&datetime模块

    在开始介绍时间模块之前先说明几点: 一. Python中常用以下几种形式表示时间 1.时间戳 2.格式化的时间字符串 3.元组(struct_time)(共九个元素),由于Python的time模块实 ...

  7. Android线程间通信的几种实现方式

    1. 通过Handler机制: private void one() { handler=new Handler(){ @Override public void handleMessage(Mess ...

  8. python3使用print打印带颜色的字符串

    一.实现过程 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制表示就是033 ...

  9. how to pass variable from shell script to sqlplus

    script sqlplus ${ORA_USR}/${ORA_PASS}@${ORA_DB} @${PARM}/TEST $new_usr $model_usr $new_pwd parm of s ...

  10. Switch能否用String类型做参数?

    switch(expr): 其中,expr参数可以是一个枚举常量(由整型或字符类型实现)或一个整数表达式,其中整数表达式可以是基本类型int或其包装类Integer.由于byte.short和char ...