TWind = class
  private
    fName, fCap:string;
    function GetCount: integer;
  public
    constructor Create;
    destructor Destroy; override;
    property Name: string read fName write fName;
    property Cap: string read fCap write fCap;
    property Count: integer read GetCount;
  end;  TWindList = class
  private
    fCount:integer;
    function GetItem(Index: Integer): TWind;//这过程怎么写?
    procedure SetItem(Index: Integer; Value: TWind);//还有这个?
  public
    constructor Create;
    destructor Destroy; override;
    function AddWind(Item: TWind): TWind; //这步最重要,要增加到Item中,然后使用时Item[0].Cap可以调用
    property Item[Index: Integer]: TWind read GetItem write SetItem; default;
    property Count: integer read fCount;
  end;

解决方案 »

  1.   

    看看TStringList是怎样写的吧!
      

  2.   

    呵呵,使用
    TCollectionItem和TCollection作为基类,专门实现这种功能的。
      

  3.   

    看不懂,主要是想解决AddWind时,如何增加到Item中调用
    上个代码改为这个
      PCmd = ^TCmd
      ^TCmd = record
        fName, fCap:string;
        fParams:array of TVarRec;
      end;
      

  4.   

    我这样写为什么在运行后执行会出错?PWind = ^TWind;
     TWind = record
       Name, Cap:string;
       Params:array of TVarRec;
     end; TWindList = class
     private
       FList: TList;
       function GetItem(Index: Integer): TWind; //how to write this procedure?
       procedure SetItem(Index: Integer; Value: TWind);
       function GetCount: integer; //This procedure?
     public
       constructor Create;
       destructor Destroy; override;
       function AddWind(Item: TWind): TWind; //Important, I want to add to Item, then can get Item[0].Cap
       property Item[Index: Integer]: TWind read GetItem write SetItem; default;
       property Count: integer read GetCount;
     end;var
     Form1: TForm1;implementation{$R *.dfm}{ TWindList }function TWindList.AddWind(Item: TWind): TWind;
    begin
     FList.Add(@Item);
    end;constructor TWindList.Create;
    begin
     FList := TList.Create;
    end;destructor TWindList.Destroy;
    begin
     FList.Free; inherited;
    end;function TWindList.GetCount: integer;
    begin
     Result := FList.Count;
    end;function TWindList.GetItem(Index: Integer): TWind;
    var
     P: PWind;
    begin
     if (Index >= 0) and (Index < Count) then
       begin
       P := FList[Index];
       Result := P^;
       end
     else
       ShowMessage('Index out of range!');
    end;procedure TWindList.SetItem(Index: Integer; Value: TWind);
    begin
     if (Index >= 0) and (Index < Count) then
       FList[Index] := @Value
     else
       ShowMessage('Index out of range!');
    end;----------------------
    procedure TForm1.Button3Click(Sender: TObject);
    var mm:TWindList;
    aa:TWind;
    begin
    mm:=TWindList.Create;
    aa.Name :='aaa';
    aa.Cap:='cap';
    aa.Params[0].VpChar :=pchar('ok');
    mm.AddCmd(aa);
    showmessage(mm.Item[0].Cap);
    mm.Free;
    end;
      

  5.   

    我觉得如果你只是要实现这个的话,没有必要自己建立的类,把TWind写成record类型,然后直接加到一个tlist中保存就可以了。尤其是TWinList你认为这个类与TList有什么区别?如果你想扩展某些功能,就应该用TList作为基类,这样可以节约很多时间和代码。面向对象编程最大的好处就是代码的重用,而delphi给我们提供的都是很好的代码,为什么不用呢。
      

  6.   

    错误描述:
    Project Project2.exe raised exception class EAccessViolation with message 'Access violation at address 004042e8 in module 'Project2.exe'. Write of ...还有为什么
    mm.Item[0].cap:='King';
    出现这个错误: Left side cannot be assigned to
      

  7.   

    将TWnd定义为Record,不如定义为Class。
    使用Record也行,但是不能像你那样,我将以你将TWnd改称Class,因为从你使用Record的方法看,我觉得你对如何使用Record不大明白,你是在用使用Class的方法来使用Record。在实现TWndList.AddCmd方法中,一定要创建TWnd实例,调用方法TWnd.Create。然后在实现TWndList.Destroy方法中,一定要销毁TWnd实例,调用方法TWnd.Free。