zoukankan      html  css  js  c++  java
  • 让自己的列表类支持遍历


    一个普通的数字列表类(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;
    

  • 相关阅读:
    activiti 用户手册中 10分钟 小例子 简单代码搭建 及 其中的 各种坑
    Eclipse启动项目正常,放到tomcat下单独启动就报错的 一例
    Project facet jst.web.jstl has not been defined.
    MySQL删除所有表的外键约束、禁用外键约束
    Unsupported major.minor version 52.0解决办法
    Could not update Activiti database schema: unknown version from database: '5.20.0.1'
    Android SDK下载地址
    Unity3d游戏中添加移动MM支付SDK问题处理
    Unity3d集成移动MM SDK 2.2的技术要点(坑爹的MM SDK)
    Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法
  • 原文地址:https://www.cnblogs.com/del/p/2041064.html
Copyright © 2011-2022 走看看