TList TObjectList的区别和使用
所在的单元
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的区别和使用的更多相关文章
- TList,TObjectList 使用——资源释放
TOjectList = Class (Tlist); TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢? 首先是 TObject 作为对象 ...
- TObjectList
AOwnsObjects = true 就是 objectlist释放的时候,里面的对象一并释放. TObjectList对象的创建方法有一个参数:constructor TObjectList.C ...
- Delphi容器类之---TList、TObjectList、TComponentList、TClassList
转载自:http://blog.csdn.net/iseekcode/article/details/4922001 从Delphi5开始VCL中增加了新的Contnrs单元,单元中定义了8个新的类, ...
- Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析
转载自:http://blog.csdn.net/jqandjq/article/details/5429137 看了这里标题,大家可能以为我会谈TListBox控件,那就错了.我要谈的是Delphi ...
- Delphi容器类之---Tlist,TStringlist,THashedStringlist的效率比较
转载自:http://www.ylzx8.cn/windows/delphi/73200.html 本人在做一个测试,服务器是IOCP的,我假定最大链接数是50000个. 测试背景:如果每个链接之间的 ...
- Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>
Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...
- 序列化TList of objects(摘自danieleteti的网站)
Some weeks ago a customer asked to me if it is possibile serialize a TList of objects. “Hey, you sho ...
- TStack,TQueue,TObjectList,TObjectStack等等
TStack,TQueue,TObjectList,TObjectStack等等,都在Contnrs.pas单元里,需要手动添加. 不同于TList类,TObjectList对象将销毁任何从列表中删除 ...
- 教程-TObjectList.Clear、TStringList.Clear方法对象有没有被释放
相关资料: http://www.cnblogs.com/rogge7/p/4631796.html delphiTStringList通过AddObject方法添加对象. object里存的只是指向 ...
随机推荐
- 利用多态,简易实现电脑usb连接设备案例
package cn.learn.Practice03; public interface UsbInterface { void open(); //打开usb void close(); //关闭 ...
- 如何配置JedisPool的参数
转自:http://blog.csdn.net/huahuagongzi99999/article/details/13631579 如何配置Pool的参数 JedisPool的配置参数很大程度上依赖 ...
- [Bzoj1051][HAOI2006]受欢迎的牛(tarjan)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051 由题意可知,被所有牛仰慕的牛之间也互相仰慕,则最后的答案一定是唯一的强连通分量,如 ...
- 在vue中设计一个客户签名的功能
直接贴代码: <template> <div class="hello"> <p>签字:</p> <canvas id=&qu ...
- BUUCTF--新年快乐
测试文件:https://buuoj.cn/files/bbf9f68a97fd551edec384914d4f3fbe/93c43c5c-3d4d-4d17-a9a1-4ffb65ebb2fb.zi ...
- 阿里云centos下搭建vsftpd,被动模式出现的问题
最近计网课设要做一个ftp服务端,所以先在自己服务器搭一个来了解一下. 首先在默认情况下连接,227 Entering Passive Mode (192,168,*,*,227,175). 显示连接 ...
- 二、jquery Try{}catch(e){}
一.Try{}catch(e){} try{ $.each($("div"),function(i,item){ if(...){ throw("异常信息"); ...
- lmbench的使用方法
一.引言 要评价一个系统的性能,通常有不同的指标,相应的会有不同的测试方法和测试工具,一般来说为了确保测试结果的公平和权威性,会选用比较成熟的商业测试软件.但在特定情形下,只是想要简单比较不同系统或比 ...
- ltp-ddt eth_iperf_tcp iperf dualtest遇到的问题
ltp-ddt eth_iperf_tcp server端:iperf -s -i 5 -w 1M client端将ddt的核心代码抠出来: iperf -c 1921.68.40.41 -m -M ...
- BZOJ4710 [Jsoi2011]分特产 容斥
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4710 题解 本来想去找一个二项式反演的题的,结果被 https://www.cnblogs.c ...