繁体   English   中英

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

[英]How can I use a class property index to assign a variable?

我创建了一个 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;

.

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

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

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

height1 := cube.FHeight;
width1 := cube.FWidth;
depth1 := cube.FDepth;

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

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

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;

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

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

在您的情况下,您可以添加公共财产

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

其中私人吸气剂 function

function GetDimension(Index: Integer): Integer;

定义为

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;

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

或者,您可以将数据存储在 static 或动态整数数组中。 然后您可以创建索引属性WidthHeightDepth并使用与数组属性相同的 getter function :

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;

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

如果将数组属性标记为default

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

您还可以编写MyShape[0]MyShape[1]MyShape[2]

注意:为简单起见,我上面的示例仅使用 getter。 但是二传手也可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM