让自己的列表类支持遍历
2021-05-01 00:16
357 查看
让自己的列表类支持遍历
一个普通的数字列表类(TNumList), 还没有支持遍历:
unit NumList; interface uses SysUtils; type TNumList = class private FCount: Integer; FNumArray: array of Double; function GetNum(aIndex: Integer): Double; procedure SetNum(aIndex: Integer; aNum: Double); public constructor Create(aCount: Integer); property Count: Integer read FCount; property Nums[Index: Integer]: Double read GetNum write SetNum; default; end; implementation { TNumList } constructor TNumList.Create(aCount: Integer); begin inherited Create; FCount := aCount; SetLength(FNumArray, FCount); end; function TNumList.GetNum(aIndex: Integer): Double; begin if (aIndex < 0) or (aIndex >= FCount) then raise Exception.Create('索引越界'); Result := FNumArray[aIndex]; end; procedure TNumList.SetNum(aIndex: Integer; aNum: Double); begin if aIndex >= FCount then begin FCount := aIndex + 1; SetLength(FNumArray, FCount); end; FNumArray[aIndex] := aNum; end; end. //end //调用测试: uses NumList; procedure TForm1.Button1Click(Sender: TObject); var nList: TNumList; i: Integer; begin nList := TNumList.Create(5); for i := 0 to nList.Count - 1 do //赋值 begin nList[i] := Random(1000) / 100; end; Memo1.Clear; for i := 0 to nList.Count - 1 do //取值 begin Memo1.Lines.Add(FloatToStr(nList[i])); end; nList.Free; end;
支持遍历的 TNumList 类:
unit NumList; interface uses SysUtils; type TNumList = class; TNumEnumerator = class private FIndex: Integer; FNumList: TNumList; public constructor Create(aNumList: TNumList); function GetCurrent: Double; function MoveNext: Boolean; property Current: Double read GetCurrent; end; TNumList = class private FCount: Integer; FNumArray: array of Double; function GetNum(aIndex: Integer): Double; procedure SetNum(aIndex: Integer; aNum: Double); public constructor Create(aCount: Integer); function GetEnumerator: TNumEnumerator; //! property Count: Integer read FCount; property Nums[Index: Integer]: Double read GetNum write SetNum; default; end; implementation { TNumList } constructor TNumList.Create(aCount: Integer); begin inherited Create; FCount := aCount; SetLength(FNumArray, FCount); end; function TNumList.GetEnumerator: TNumEnumerator; begin Result := TNumEnumerator.Create(Self); end; function TNumList.GetNum(aIndex: Integer): Double; begin if (aIndex < 0) or (aIndex >= FCount) then raise Exception.Create('索引越界''); Result := FNumArray[aIndex]; end; procedure TNumList.SetNum(aIndex: Integer; aNum: Double); begin if aIndex >= FCount then begin FCount := aIndex + 1; SetLength(FNumArray, FCount); end; FNumArray[aIndex] := aNum; end; { TNumEnumerator } constructor TNumEnumerator.Create(aNumList: TNumList); begin inherited Create; FIndex := -1; FNumList := aNumList; end; function TNumEnumerator.GetCurrent: Double; begin Result := FNumList[FIndex]; end; function TNumEnumerator.MoveNext: Boolean; begin Result := FIndex < FNumList.Count - 1; if Result then Inc(FIndex); end; end. //end //调用测试 uses NumList; procedure TForm1.Button1Click(Sender: TObject); var nList: TNumList; i: Integer; num: Double; begin nList := TNumList.Create(5); for i := 0 to nList.Count - 1 do //赋值 begin nList[i] := Random(1000) / 100; end; Memo1.Clear; for num in nList do //遍历 begin Memo1.Lines.Add(FloatToStr(num)); end; nList.Free; end;
相关文章推荐
- 善用泛型数组
- WCF揭秘——可靠性会话功能
- ActionScript 3.0 记要(2): 类与接口
- 理解浮点数的储存规则
- 运维安全应该怎么做?
- 通过消息跨进程发送与接收 TCopyDataStruct 数据 - 回复 "skymesh"
- 使用多窗体时, 关于节约内存和加快启动速度的思考与尝试
- 从哪查找当前程序所有可用的环境变量?
- 关于 "高位" 与 "低位" - 回复 "Lovemit" 的问题
- 关于 OnMouseWheelDown、OnMouseWheelUp 等事件的简单例子 - 回复
- 如何用弹出窗口显示进度 - 回复
- 把字节数组读入到流 - 回复
- 动态调整 TBitBtn 上的图片 - 回复
- WCF揭秘——简单的WCF开发实例
- 汇编指令速查
- 如何让 TreeView 的列表项携带数据、并读出或删除这个数据 - 回复
- 获取 "斐波那契数列" 的函数
- 枚举可以这样遍历
- 如何用产品思维摆地摊月入三万?
- 简单的 "双缓冲" 绘图的例子 - 回复 "TookiQ" 的问题