现在准备一步步地模拟 TList 类, 建立一个自己的 TMyList.

首先, 这个类中应该包括前面提到的那个 Pointer 数组(TPointerList)的指针(PPointerList):


TMyList = class(TObject)
  FList: PPointerList;
end;

既然是一个列表, 应该有 Count 字段:

TMyList = class(TObject)
  FList: PPointerList;
  FCount: Integer;
end;

TList 类还有一个 FCapacity 字段.

譬如当前列表中有 10 个元素时, 其实列表已经提前申请出更多位置, 这样就不必每次添加每次申请内存, 从而提高了效率.

这个 FCapacity 字段就表示已申请的可放置元素位置的总数.


TMyList = class(TObject)
  FList: PPointerList;
  FCount: Integer;
  FCapacity: Integer;
end;

它们都应该是私有的:

TMyList = class(TObject)
private
  FList: PPointerList;
  FCount: Integer;
  FCapacity: Integer;
public
end;

应该用属性操作它们:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs; type
  TForm1 = class(TForm)
  end;
{--------------------------------}
  TMyList = class(TObject)
  private
    FList: PPointerList;
    FCount: Integer;
    FCapacity: Integer;
  public
    property Capacity: Integer; {光标放在属性上, 执行 Shift+Ctrl+C, 自动建立属性}
    property Count: Integer;
    property List: PPointerList;
  end;
{--------------------------------} var
  Form1: TForm1; implementation {$R *.dfm} end.

自动建立后的属性:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs; type
  TForm1 = class(TForm)
  end;
{--------------------------------}
  TMyList = class(TObject)
  private
    FList: PPointerList;
    FCount: Integer;
    FCapacity: Integer;
    procedure SetCapacity(const Value: Integer);
    procedure SetCount(const Value: Integer);
    procedure SetList(const Value: PPointerList);
  public
    property Capacity: Integer read FCapacity write SetCapacity;
    property Count: Integer read FCount write SetCount;
    property List: PPointerList read FList write SetList;
  end;
{--------------------------------} var
  Form1: TForm1; implementation {$R *.dfm} { TMyList } procedure TMyList.SetCapacity(const Value: Integer);
begin
  FCapacity := Value;
end; procedure TMyList.SetCount(const Value: Integer);
begin
  FCount := Value;
end; procedure TMyList.SetList(const Value: PPointerList);
begin
  FList := Value;
end; end.

在 TList 中, SetCount 和 SetCapacity 方法都是放在 protected 区以供子类使用, 这里暂时不考虑继承的问题, 就让它们先在 private 区吧;

不过 List 属性应该是只读的, 它就是那个核心数组的指针, 对它的赋值应该通过更多其他的方法.

修改如下:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs; type
  TForm1 = class(TForm)
  end;
{--------------------------------}
  TMyList = class(TObject)
  private
    FList: PPointerList;
    FCount: Integer;
    FCapacity: Integer;
    procedure SetCapacity(const Value: Integer);
    procedure SetCount(const Value: Integer);
  public
    property Capacity: Integer read FCapacity write SetCapacity;
    property Count: Integer read FCount write SetCount;
    property List: PPointerList read FList;
  end;
{--------------------------------} var
  Form1: TForm1; implementation {$R *.dfm} { TMyList } procedure TMyList.SetCapacity(const Value: Integer);
begin
  FCapacity := Value;
end; procedure TMyList.SetCount(const Value: Integer);
begin
  FCount := Value;
end; end.

作为一个列表, 最起码应该有 Add、Delete、Clear 方法.

Add 方法的参数应该是一个指针(TList 就是存放指针的吗), 如果需要同时返回元素的位置, 应该定义成函数;

Delete 是删除指定位置上的元素, 参数是个位置号;

Clear 是清除列表, 对象释放(Destroy)时也应该执行此过程.

综上所述, 修改如下:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs; type
  TForm1 = class(TForm)
  end;
{--------------------------------}
  TMyList = class(TObject)
  private
    FList: PPointerList;
    FCount: Integer;
    FCapacity: Integer;
    procedure SetCapacity(const Value: Integer);
    procedure SetCount(const Value: Integer);
  public
    destructor Destroy; override;        {覆盖父类的 Destroy 方法}
    function Add(Item: Pointer): Integer;
    procedure Clear;                      {在 TList 中这是个虚方法, 以备子类覆盖}
    procedure Delete(Index: Integer);
    property Capacity: Integer read FCapacity write SetCapacity;
    property Count: Integer read FCount write SetCount;
    property List: PPointerList read FList;
  end;
{--------------------------------} var
  Form1: TForm1; implementation {$R *.dfm} { TMyList } function TMyList.Add(Item: Pointer): Integer;
begin end; procedure TMyList.Clear;
begin end; procedure TMyList.Delete(Index: Integer);
begin end; destructor TMyList.Destroy;
begin
  Clear;    {在类释放时同时清除列表}
  inherited; {在 TList 中没有这句, 大概是因为它的父类也没什么可继承的}
end; procedure TMyList.SetCapacity(const Value: Integer);
begin
  FCapacity := Value;
end; procedure TMyList.SetCount(const Value: Integer);
begin
  FCount := Value;
end; end.

为了让代码更清晰, 还是把这个类相关的所有代码放到一个独立的单元吧(新建一个单元, 我们把它保存为: MyList.pas).

因为 MaxListSize、TPointerList、PPointerList 都是在 Classes 单元定义的, 为了省略 uses Classes, 在 MyList 单元中重新做了定义.


unit MyList;

interface

const
  MaxListSize = Maxint div ; type
  PPointerList = ^TPointerList;
  TPointerList = array[..MaxListSize - ] of Pointer;   TMyList = class(TObject)
  private
    FList: PPointerList;
    FCount: Integer;
    FCapacity: Integer;
    procedure SetCapacity(const Value: Integer);
    procedure SetCount(const Value: Integer);
  public
    destructor Destroy; override;
    function Add(Item: Pointer): Integer;
    procedure Clear;
    procedure Delete(Index: Integer);
    property Capacity: Integer read FCapacity write SetCapacity;
    property Count: Integer read FCount write SetCount;
    property List: PPointerList read FList;
  end; implementation { TMyList } function TMyList.Add(Item: Pointer): Integer;
begin end; procedure TMyList.Clear;
begin end; procedure TMyList.Delete(Index: Integer);
begin end; destructor TMyList.Destroy;
begin
  Clear;
  inherited;
end; procedure TMyList.SetCapacity(const Value: Integer);
begin end; procedure TMyList.SetCount(const Value: Integer);
begin end; end.

至此, 类的轮廓已经出来, 该实现一下这些方法了.

学习 TList 类的实现[4]的更多相关文章

  1. 学习 TList 类的实现[8]

    现在准备建立 Items 数组属性; 在 public 区输入下面代码:property Items[Index: Integer]: Pointer; 执行 Shift+Ctrl+C 后的代码是: ...

  2. 学习 TList 类的实现[1]

    最近整理了一些函数列表, 算是一个宏观的安排; 等以后再碰到一些函数时就可以放置的更有次序一些. 我对函数与类的理解是: 函数是一个功能模块, 类是一个更强大的功能模块; Delphi 已经提供了很多 ...

  3. 学习 TList 类的实现[2]

    我原来以为 TList 可能是一个链表, 其实只是一个数组而已. 你知道它包含着多大一个数组吗? MaxListSize 个!MaxListSize 是 Delphi 在 Classes 单元定义的一 ...

  4. 学习 TList 类的实现[6]

    实现 TMyList.Add 函数. TList 中的 Add 函数用到了一个 Grow 方法, 它的原理是元素越多就为以后准备更多内存, 我们这里省略为预留 4 个元素的内存; TList 中的 A ...

  5. 学习 TList 类的实现[5]

    先来实现 TMyList.SetCapacity. 马上会想到下面代码: procedure TMyList.SetCapacity(const Value: Integer); begin   if ...

  6. 学习 TList 类的实现[3] - 不能回避的话题: 内存分配

    在 Delphi 中, 几乎所有的类型都有对应的指针类型, 譬如: Char PChar Word PWORD Double PDouble TPoint PPoint 甚至一种类型对应这着几种指针类 ...

  7. 学习 TList 类的实现[7]

    总结目前 TMyList 已具备的功能(3 个方法.3 个属性): Add: 添加; Delete: 删除; Clear: 清空;Count: 元素总数;Capacity: 已存在的所有元素位置数;L ...

  8. Java虚拟机JVM学习07 类的卸载机制

    Java虚拟机JVM学习07 类的卸载机制 类的生命周期 当Sample类被加载.连接和初始化后,它的生命周期就开始了. 当代表Sample类的Class对象不再被引用,即不可触及时,Class对象就 ...

  9. Java虚拟机JVM学习04 类的初始化

    Java虚拟机JVM学习04 类的初始化 类的初始化 在初始化阶段,Java虚拟机执行类的初始化语句,为类的静态变量赋予初始值. 在程序中,静态变量的初始化有两种途径: 1.在静态变量的声明处进行初始 ...

随机推荐

  1. Vector3.Set的正确使用

    直接调用position.Set会发现没有用.其实和其本身是结构体有关,要当成一个值类型来看待,因为他全部是在栈中. transform.position.Set(,,); 改成这样即可: var t ...

  2. SQL 从查询结果里查询

    有orders表: 我想要从从表中查出每天电动车和手机各自的销售总额.这个需求还是蛮简单的,仅仅须要依据createtime和product group by即可了.以下是我写的SQL语句: SELE ...

  3. vim 学习日志(6):分屏 sp,vsp,分屏移动

    Vim的分屏功能 本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -o ...

  4. FZU Problem 2028 时空门问题

    Problem Description 在一个N*M的地图上旅行.地图上有些地方可以走用. 表示,不能走用 # 表示.在可以走的地方上下左右移动一格需要一个单位时间.可以走的地方还有一些时空之门.时空 ...

  5. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  6. c++多态之——vptr指针

    之前做过一个测试,在一个类中定义一个virtual修饰的函数时,sizeof这个类,发现类的大小多了恰好一个指针的字节大小,当初不明白,只是记住有这么一个特性.后来,发现它就是c++编译器给我们添加的 ...

  7. MyEclipse9.0激活步骤

    MyEclipse 9.0的激活机制 1.破解公钥(确保MyEclipse没有开启,否则失败!)   解压破解包中的myeclipse9_keygen_activator.zip,复制publicKe ...

  8. 一款基于jquery漂亮的按钮

    之前为大家分享了好多css3实现的按钮.今天给大家分享一款基于jquery漂亮的按钮.这款按钮背景下用了一张图片.当鼠标经过的时候背景用半透明div遮住.一起看下效果图: 在线预览   源码下载 实现 ...

  9. BSD Socket~TCP~Example Code

    TCP 协议实现 C版本号,可用于Mac OS X机器上执行 Server: /* Setting up a simple TCP server involves the following step ...

  10. Spring中@Resource与@Autowired

    问题 这其实就是@Autoware与@Resource没有正确的使用,这个错误是因为wmPoiOplogService这个变量装配方式是@Resource,按照@Resource的按名字查找的方式,并 ...