0

继上一个问题之后,当我尝试编译它时,我在这一行得到错误不兼容类型:

    Property player[i : integer] : TStringList read p;

我不确定为什么?这是全班:

unit Battle;

interface

uses
  SysUtils,Dialogs,Classes,inifiles, StdCtrls;
type

  TPlayers = class
  Private
    p : array[1..20] of TStringList;
    FPlaceUnit: Boolean;
    FTeamCount: Integer;
  Public
    Property player[i : integer] : TStringList read p;
    property PlaceUnit : Boolean read FPlaceUnit write FPlaceUnit;
    procedure AddPlayer (PlayerNo : integer; player : String);
    property TeamCount : Integer read FTeamCount write FTeamCount;

    constructor Create;   virtual;
  End;


{Host class}
  THostPlayers = Class(TPlayers)
  Private
    FIsHost: string;
  Public
    constructor Create; override;
    property IsHost : string read FIsHost write FIsHost;
 End;


{Guest Class}
  TGuestPlayers = Class(TPlayers)
  Private
    FIsGuest: string;
  Public
    constructor Create; override;
    property IsGuest : string read FIsGuest write FIsGuest;
  End;

implementation

uses
main;
{constructor}
constructor TPlayers.Create;
begin
  p := TStringList.Create;
end;
constructor THostPlayers.Create;
begin
  inherited;  // Calls TPlayers.Create
  IsHost := 'No';
  PlaceUnit := true;
  TeamCount :=0;
end;
constructor TGuestPlayers.Create;
begin
  inherited;  // Calls TPlayers.Create
  IsGuest := 'No';
  PlaceUnit := true;
  TeamCount := 0;
end;

{ADD Player}
procedure TPlayers.AddPlayer(PlayerNo : integer; player : String);
  var
      CharINI : TIniFile;
  begin
      CharINI := Tinifile.Create(thisdir+'\char\charstats.ini');
      CharINI.ReadSectionValues(player,player[PlayerNo]);
      CharINI.Free;
  end;
end.
4

1 回答 1

2

首先,属性“返回”一个 TStringList 并且字段 p 是一个 TStringlist 数组,所以这就是你得到不兼容类型错误的原因。

你会期望:

// This is not working
Property player[i : integer] : TStringList read p[i];  // Not supported...

解决这个问题。但是你不能直接访问数组元素,所以你需要一个 getter 函数:

TPlayers = class
private
  function GetPlayer(i: integer): TStringList;

public
  Property player[i : integer] : TStringList read GetPlayer 
end;



function TPlayers.GetPlayer(i: integer): TStringList;
begin
  Result := p[i];
end;
于 2012-05-26T13:16:20.747 回答