如何使用类属性索引来分配变量?

Jsk*_*Jsk 1 delphi properties class

我创建了一个类:

type
      TShape = class
      private
        FHeight: Integer;
        FWidth: Integer;
        FDepth: Integer;

      public
        constructor CreateShape(AHeight: Integer; AWidth: Integer; ADepth: Integer);

        property height: Integer index 0 read FHeight write FHeight;
        property width: Integer index 1 read FWidth write FWidth;
        property depth: Integer index 2 read FDepth write FDepth;

      end;
Run Code Online (Sandbox Code Playgroud)

.

constructor TShape.CreateShape(AHeight: Integer; AWidth: Integer;
  ADepth: Integer);
begin
  inherited Create;
  FHeight := AHeight;
  FWidth := AWidth;
  FDepth := ADepth;
end;
Run Code Online (Sandbox Code Playgroud)

目前我通过使用属性名称分配变量来分配值:

cube := TShape.CreateShape(5, 5, 5);

height1 := cube.FHeight;
width1 := cube.FWidth;
depth1 := cube.FDepth;
Run Code Online (Sandbox Code Playgroud)

但是我如何使用索引而不是名称来分配属性,height1 := cube.FHeight而不是height1 := cube[0]

And*_*and 6

我认为您误解了index说明符的工作方式。它们允许您对多个属性使用单个getter 或 setter 函数:

TTest = class
private
  function GetColor(AIndex: Integer): TColor;
public
  property BackgroundColor: TColor index 0 read GetColor;
  property ForegroundColor: TColor index 1 read GetColor;
end;

// ...

function TTest.GetColor(AIndex: Integer): TColor;
begin
  case AIndex of
    0:
      Result := clRed; // background colour
    1:
      Result := clBlue; // foreground colour
  else
    Result := clBlack;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

因此,它只能与 getter 和 setter 函数一起使用;您不能使用字段。

您似乎对不同的东西感兴趣,一个数组属性,另外还有default. 数组属性是一个属性,它是对象用户的数组(如Memo1.Lines[4])。因此,它是一个单一的属性,它是一个数组。

在你的情况下,你可以添加一个公共财产

property Dimensions[Index: Integer]: Integer read GetDimension;
Run Code Online (Sandbox Code Playgroud)

私有 getter 函数在哪里

function GetDimension(Index: Integer): Integer;
Run Code Online (Sandbox Code Playgroud)

被定义为

function TShape.GetDimension(Index: Integer): Integer;
begin
  case Index of
    0:
      Result := FHeight;
    1:
      Result := FWidth;
    2:
      Result := FDepth;
  else
    Result := 0; // or raise an exception
  end;
end;
Run Code Online (Sandbox Code Playgroud)

这仍然会使用您的FHeight,FWidthFDepth字段来存储数据。

或者,您可以将数据存储在静态或动态整数数组中。然后,您可以创建索引的属性WidthHeight以及Depth与使用相同的getter函数作为数组属性:

type
  TShape = class
  private
    FDimensions: array[0..2] of Integer;
    function GetDimension(Index: Integer): Integer;
  public
    constructor CreateShape(AHeight: Integer; AWidth: Integer; ADepth: Integer);

    property Height: Integer index 0 read GetDimension;
    property Width: Integer index 1 read GetDimension;
    property Depth: Integer index 2 read GetDimension;

    property Dimensions[Index: Integer]: Integer read GetDimension;

  end;

// ...

{ TShape }

constructor TShape.CreateShape(AHeight, AWidth, ADepth: Integer);
begin
  FDimensions[0] := AHeight;
  FDimensions[1] := AWidth;
  FDimensions[2] := ADepth;
end;

function TShape.GetDimension(Index: Integer): Integer;
begin
  if InRange(Index, Low(FDimensions), High(FDimensions)) then
    Result := FDimensions[Index]
  else
    raise Exception.CreateFmt('Invalid dimension index: %d', [Index]);
end;
Run Code Online (Sandbox Code Playgroud)

现在,您可以访问MyShape.HeightMyShape.WidthMyShape.Depth,以及MyShape.Dimensions[0]MyShape.Dimensions[1]MyShape.Dimensions[2]

如果您将数组属性标记为default

property Dimensions[Index: Integer]: Integer read GetDimension; default;
Run Code Online (Sandbox Code Playgroud)

你也可以写MyShape[0], MyShape[1], 和MyShape[2]

注意:为简单起见,我上面的示例仅使用 getter。但是 setter 也能工作。