[英]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;
這仍然會使用您的FHeight
、 FWidth
和FDepth
字段來存儲數據。
或者,您可以將數據存儲在 static 或動態整數數組中。 然后您可以創建索引屬性Width
、 Height
和Depth
並使用與數組屬性相同的 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.Height
、 MyShape.Width
和MyShape.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.