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里存的只是指向 ...
随机推荐
- Stream的排序
1.list<Integer>的正序 List<Integer> list = new ArrayList<>();list.add(50);list.add(45 ...
- ASP精华[转]
<% '#######以下是一个类文件,下面的注解是调用类的方法################################################ '# 注意:如果系统不支持建立S ...
- 洛谷 P5019 铺设道路 & [NOIP2018提高组](贪心)
题目链接 https://www.luogu.org/problem/P5019 解题思路 一道典型的贪心题. 假设从左往右填坑,如果第i个深与第i+1个,那么第i+1个就不需要额外填: 如果第i+1 ...
- Warning: session_start(): open(/var/lib/php/session/)
Warning: session_start(): open(/var/lib/php/session/) 今天放置一个新的站点www.96net.com.cn在里面,登陆后台出现这种错,之后再lin ...
- 故事版(storyBoard)-lllegal configuration connection cannot have a prototype objct as
今天下午做项目的时候.居然出了一个太不是问题的问题了,这个错误太低级了. lllegal configuration connection 'flagImg' cannot have a protot ...
- k3 cloud成本调整单提示期末余额不存在调整单分录的维度,请先出库核算确认是否存在核算维度的数据
成本调整单提示期末余额不存在调整单分录的维度,请先出库核算确认是否存在核算维度的数据,如下图所示: 解决办法:先做出库核算,然后做成本调整单,再做出库核算(出库成本核算)
- vue.js(6)--v-model
v-model实现数据的双向绑定(简易计算器实例) 简易计算器实例 <!DOCTYPE html> <html lang="en"> <head> ...
- 四、附加到进程调试(.NET Core)
1.安装.net core windows server托管工具包: 1.下载https://dotnet.microsoft.com/download/thank-you/dotnet-runtim ...
- python数字图像处理(三)边缘检测常用算子
在该文将介绍基本的几种应用于边缘检测的滤波器,首先我们读入saber用来做为示例的图像 #读入图像代码,在此之前应当引入必要的opencv matplotlib numpy saber = cv2.i ...
- Java实现sock5代理服务器
入职练手socks5代理服务器,过程总结一下. 1.下载火狐浏览器,设定代理为socks5代理,地址为127.0.0.1:1080. 2.socks5协议1928,中文版,原版,认真阅读 3.按照协议 ...