我已经开始在Delphi XE中使用TCollection类,并且在Delphi中的Using TOwnedCollection后代中找到了答案,这是一个很好的起点。TCollection管理TCollectionItems的列表。但是我注意到TCollection.Add似乎没有将TCollectionItem添加到Collections数组,实际上我的测试似乎证实了这一点。TCollection本身的代码是:
function TCollection.Add: TCollectionItem;
begin
Result := FItemClass.Create(Self);
Added(Result);
end;
Run Code Online (Sandbox Code Playgroud)
FItemClass是将创建的对象类型,我认为已添加到TCollection对象中。不推荐使用Added()方法,它似乎是一个旧的通知方法。我在哪里都看不到将结果添加到集合中。如何将TCollectionItem添加到TCollection?
TCollectionItem.Create(Collection: TCollection)调用TCollectionItem.SetCollection,将项目添加到Collection:
procedure TCollectionItem.SetCollection(Value: TCollection);
begin
if FCollection <> Value then
begin
if FCollection <> nil then FCollection.RemoveItem(Self);
if Value <> nil then Value.InsertItem(Self);
end;
end;
Run Code Online (Sandbox Code Playgroud)
TCollection.Add之所以调用它是因为它创建了集合项目,并且项目构造函数将项目添加到了包含的集合中。因此TCollection.Add确实可以创建项目(在流程中将其添加到自身中),并返回对该新创建项目的引用。(您可以使用该引用来设置TCollectionItem自身的属性。)
| 归档时间: |
|
| 查看次数: |
1048 次 |
| 最近记录: |