现在准备建立 Items 数组属性; 在 public 区输入下面代码:
property Items[Index: Integer]: Pointer;

执行 Shift+Ctrl+C 后的代码是:


...

  TMyList = class(TObject)
  private
    ...
    function GetItems(Index: Integer): Pointer;
    procedure SetItems(Index: Integer; const Value: Pointer);
  public
    ...
    property Items[Index: Integer]: Pointer read GetItems write SetItems;
  end; implementation { TMyList } ... function TMyList.GetItems(Index: Integer): Pointer;
begin end; procedure TMyList.SetItems(Index: Integer; const Value: Pointer);
begin end; end.

在 TList 类中, GetItems 方法被命名为 Get; SetItems 方法被命名为 Put. 这里我们就不准备改名了.

分别实现如下:


function TMyList.GetItems(Index: Integer): Pointer;
begin
  if (Index < ) or (Index >= FCount) then
    raise Exception.CreateFmt('异常:%d', [Index]);
  Result := FList^[Index];
end; {同前, 在这里我们也没有触动 Notify 方法, 现在的 TMyList 也没有这个方法}
procedure TMyList.SetItems(Index: Integer; const Value: Pointer);
begin
  if (Index < ) or (Index >= FCount) then
    raise Exception.CreateFmt('异常:%d', [Index]);   if Value <> FList^[Index] then
    FList^[Index] := Value;
end;

至此, 我们可以使用 List.Itmes[i] 的方式访问列表中的元素了;

再进一步, 让它成为默认属性吧; 尽管只能选择一个属性为默认属性, 但哪一个属性能比它更重要的呢?


//只需把在 public 区声明的:
property Items[Index: Integer]: Pointer read GetItems write SetItems; //改为:
property Items[Index: Integer]: Pointer read GetItems write SetItems; default;

Items 就是默认属性了, 这样再访问一个元素时, 即可以用: List.Itmes[i]; 也可以使用: List[i]. 默认属性真方便.

看看 TMyList 类目前的全部代码:


unit MyList;

interface

uses SysUtils;

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);
    function GetItems(Index: Integer): Pointer;
    procedure SetItems(Index: Integer; const Value: Pointer);
  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;
    property Items[Index: Integer]: Pointer read GetItems write SetItems; default;
  end; implementation { TMyList } function TMyList.Add(Item: Pointer): Integer;
begin
  if FCount = FCapacity then SetCapacity(FCapacity + );
  FList^[FCount] := Item;
  Result := FCount;
  Inc(FCount);
end; procedure TMyList.Clear;
begin
  SetCount();
  SetCapacity();
end; procedure TMyList.Delete(Index: Integer);
begin
  if (Index < ) or (Index >= FCount) then
    raise Exception.CreateFmt('非法的 Index:%d', [Index]);
  if Index < FCount then
    System.Move(FList^[Index+], FList^[Index], (FCount-Index)* SizeOf(Pointer));
  Dec(FCount);
end; destructor TMyList.Destroy;
begin
  Clear;
  inherited;
end; procedure TMyList.SetCapacity(const Value: Integer);
begin
  if (Value < FCount) or (Value > MaxListSize) then
    raise Exception.CreateFmt('非法数据:%d', [Value]);
  if FCapacity <> Value then
  begin
    ReallocMem(FList, Value * SizeOf(Pointer));
    FCapacity := Value;
  end;
end; procedure TMyList.SetCount(const Value: Integer);
var
  i: Integer;
begin
  if (Value < ) or (Value > MaxListSize) then
    raise Exception.CreateFmt('非法数据:%d', [Value]);
  if Value > FCapacity then SetCapacity(Value);
  if Value > FCount then
    FillChar(FList^[FCount], (Value - FCount) * SizeOf(Pointer), )
  else
    for i := FCount - downto Value do
      Delete(I);
  FCount := Value;
end; function TMyList.GetItems(Index: Integer): Pointer;
begin
  if (Index < ) or (Index >= FCount) then
    raise Exception.CreateFmt('异常:%d', [Index]);
  Result := FList^[Index];
end; procedure TMyList.SetItems(Index: Integer; const Value: Pointer);
begin
  if (Index < ) or (Index >= FCount) then
    raise Exception.CreateFmt('异常:%d', [Index]);   if Value <> FList^[Index] then
    FList^[Index] := Value;
end; end.

现在访问元素方便了, 重做上一个测试:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls; type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end; var
  Form1: TForm1;
implementation {$R *.dfm} uses MyList; type
  TMyRec = record
    name: string[];
    age : Word;
  end; procedure TForm1.FormCreate(Sender: TObject);
var
  ListA: TMyList;
  r,r1,r2,r3,r4,r5: TMyRec;
begin
  ListA := TMyList.Create;   r1.name := '张三';
  r1.age  := ;
  ListA.Add(@r1);   r2.name := '李四';
  r2.age  := ;
  ListA.Add(@r2);   r3.name := '王五';
  r3.age  := ;
  ListA.Add(@r3);   r4.name := '孙六';
  r4.age  := ;
  ListA.Add(@r4);   r5.name := '候七';
  r5.age  := ;
  ListA.Add(@r5);   {获取第三个元素}
  //r := TMyRec(ListA.List^[2]^);          {这是以前的代码}
  r := TMyRec(ListA[]^);
  ShowMessageFmt('%s:%d',[r.name, r.age]); {王五:33}   {删除第三个元素后再访问第三个元素}
  ListA.Delete();
  //r := TMyRec(ListA.List^[2]^);          {这是以前的代码}
  r := TMyRec(ListA[]^);
  ShowMessageFmt('%s:%d',[r.name, r.age]); {孙六:44}   {现在通过 Items 属性, 不仅可以取值, 还可以赋值}
  ListA[] := @r1;
  r := TMyRec(ListA[]^);
  ShowMessageFmt('%s:%d',[r.name, r.age]); {张三:11}   ListA.Free;
end; end.

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

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

    现在准备一步步地模拟 TList 类, 建立一个自己的 TMyList. 首先, 这个类中应该包括前面提到的那个 Pointer 数组(TPointerList)的指针(PPointerList): ...

  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. 【Android】3.19 示例19--全景图HelloWorld

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 百度全景图是一种实景地图服务.为用户提供城市.街道和其他环境的360度全景图像,用户可以通过该服务获得如临其境 ...

  2. HAProxy负载均衡原理及企业级实例部署haproxy集群

    一 HAProxy简介   HAProxy是一种高效.可靠.免费的高可用及负载均衡解决方案,非常适合于高负载站点的七层数据请求.客户端通过HAProxy代理服务器获得站点页面,而代理服务器收到客户请求 ...

  3. Django内置过滤器详解附代码附效果图--附全部内置过滤器帮助文档

    前言 基本环境 Django版本:1.11.8 Python版本:3.6 OS: win10 x64 本文摘要 提供了常用的Django内置过滤器的详细介绍,包括过滤器的功能.语法.代码和效果示例. ...

  4. TagsView.vue

    1.TagsView.vue <template> <div class="tags-view-container"> <scroll-pane cl ...

  5. Brocade300 commands

    aaaconfig                  Configure RADIUS for AAA servicesad                         Specifies all ...

  6. registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

    问题是tomcat的版本问题,tomcat新检测机制导致的这个问题,换版本可以解决问题,但不建议这么做,租用服务器不是你说换就换的.其实问题根源是BasicDataSource,BasicDataSo ...

  7. 几行css3代码实现超炫加载动画

    之前为大家分享了css3实现的加载动画.今天为大家带来一款只需几行代码就可以实现超炫的动画加载特效.我们一起看下效果图: 在线预览   源码下载 实现代码: 极简的html代码: <div> ...

  8. 设置DatagridView的列头样式

    设置DataGridView.ColumnHeaderDefaultCellStyle的BackColor属性会发现没有效果.这是因为在启动了可视样式的时候,BackColor和ForeColor的值 ...

  9. Java学习理解路线图

    信息来自知乎网友 学习截图:来自开源力量

  10. 【3C认证】安防产品3C认证

    安防产品3C认证作为3C认证的一部分.安防产品即:防范的手段达到或实现安全的目的的设备或器材. 依据<中华人民共和国产品质量法>.<中华人民共和国标准化法>.<中华人民共 ...