zoukankan      html  css  js  c++  java
  • TObjectList

    AOwnsObjects = true 就是  objectlist释放的时候,里面的对象一并释放。

     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的影响。我就常常用 TObjectList来管理对象,很方便。记得第一次用时,没看文档,用的默认参数值,重载其释放方法,结果一直报错,因为在DLL中实现,找了很久才找出缘由。

    TList  TObjectList的区别和使用。
    
    1,所在的单元。
    
    TList一直就是在Classes里
    TObjectList一直就是在Contnrs里
    
    2, 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里的对象一块释放掉.
    
    TList,TObjectList 使用——资源释放
    
    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 := 0 to 6 do  
      begin  
        btn := TButton.Create(Self);  
        with btn do begin  
          Caption := Format('Btn %d', [i+1]);  
          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 := 0 to 6 do  
      begin  
        btn := TButton.Create(Self);  
        with btn do begin  
          Caption := Format('Btn %d', [i+1]);  
          Parent := Self;  
        end;  
        list.Add(btn);  
      end;  
      ShowMessage('TList 释放后, 其中的对象并未释放');  
      list.Free;  
    end; 
    如果用TObjectList来存储,当调用sort方法时要注意先将owerobject设置为False
    总结:如果有一个对象需要用到列表,最好从 TOjectList 开始继承,对资源释放有完善的处理机制。
  • 相关阅读:
    ajax面试题
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/del88/p/6877324.html
Copyright © 2011-2022 走看看