简体   繁体   English

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

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

I have created a class:我创建了一个 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;

And currently I assign the values by using the name of the property to assign a variable:目前我通过使用属性名称来分配值来分配变量:

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

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

But how do I use the index instead of the name to assign a property, so height1:= cube.FHeight would instead be height1:= cube[0] ?但是如何使用索引而不是名称来分配属性,所以height1:= cube.FHeight将改为height1:= cube[0]

I think you have misunderstood how index specifiers work.我认为您误解了index说明符的工作方式。 They allow you to use a single getter or setter function for several properties:它们允许您对几个属性使用单个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;

Hence, it can only be used with getter and setter functions;因此,它只能与 getter 和 setter 函数一起使用; you cannot use fields.你不能使用字段。

You seem to be interested in something different, an array property , which is in addition default .您似乎对不同的东西感兴趣,一个数组属性,它也是default An array property is a property that is an array to the object's user (like Memo1.Lines[4] ).数组属性是对象用户的数组属性(如Memo1.Lines[4] )。 Hence, it is a single property which is an array.因此,它是一个单一的属性,它是一个数组。

In your case, you could add a public property在您的情况下,您可以添加公共财产

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

where the private getter function其中私人吸气剂 function

function GetDimension(Index: Integer): Integer;

is defined as定义为

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;

This would still use your FHeight , FWidth , and FDepth fields to store the data under the hood.这仍然会使用您的FHeightFWidthFDepth字段来存储数据。

Alternatively, you could store the data in a static or dynamic array of integers.或者,您可以将数据存储在 static 或动态整数数组中。 Then you could create indexed properties Width , Height , and Depth and use the same getter function as for the array property:然后您可以创建索引属性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;

Now you can access MyShape.Height , MyShape.Width , and MyShape.Depth , as well as MyShape.Dimensions[0] , MyShape.Dimensions[1] , and MyShape.Dimensions[2] .现在您可以访问MyShape.HeightMyShape.WidthMyShape.Depth以及MyShape.Dimensions[0]MyShape.Dimensions[1]MyShape.Dimensions[2]

If you mark the array property as default ,如果将数组属性标记为default

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

you can also write MyShape[0] , MyShape[1] , and MyShape[2] .您还可以编写MyShape[0]MyShape[1]MyShape[2]

Note: For simplicity, my examples above only use getters.注意:为简单起见,我上面的示例仅使用 getter。 But setters work as well.但是二传手也可以。

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

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