是否可以对属性使用相同的 getter 和 setter?
Is it possible to use the same getter and setter for properties?
我有一个class有多个变量,可以自己访问属性:
TGame = class(TObject)
strict private
FValue1 : Integer;
FValue2 : Integer;
private
procedure SetValue1(const Value : Integer);
procedure SetValue2(const Value : Integer);
function GetValue1() : Integer;
function GetValue2() : Integer;
public
property Value1 : Integer read GetValue1 write SetValue1;
property Value2 : Integer read GetValue2 write SetValue2;
我想知道,是否有办法对不同的属性使用相同的 getter 和 setter,如下所示:
property Value1 : Integer read GetValue write SetValue;
property Value2 : Integer read GetValue write SetValue;
是的,这可以使用 index specifiers:
来实现
Index specifiers allow several properties to share the same access method while representing different values.
例如,
type
TTest = class
strict private
FValues: array[0..1] of Integer;
function GetValue(Index: Integer): Integer;
procedure SetValue(Index: Integer; const Value: Integer);
public
property Value1: Integer index 0 read GetValue write SetValue;
property Value2: Integer index 1 read GetValue write SetValue;
end;
{ TTest }
function TTest.GetValue(Index: Integer): Integer;
begin
Result := FValues[Index];
end;
procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
FValues[Index] := Value;
end;
当然,这也适用于您原来的私有字段:
type
TTest = class
strict private
FValue1: Integer;
FValue2: Integer;
function GetValue(Index: Integer): Integer;
procedure SetValue(Index: Integer; const Value: Integer);
public
property Value1: Integer index 1 read GetValue write SetValue;
property Value2: Integer index 2 read GetValue write SetValue;
end;
{ TTest }
function TTest.GetValue(Index: Integer): Integer;
begin
case Index of
1:
Result := FValue1;
2:
Result := FValue2;
else
raise Exception.Create('Invalid index.');
end;
end;
procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
case Index of
1:
FValue1 := Value;
2:
FValue2 := Value;
end;
end;
但你似乎更愿意需要 array property:
type
TTest = class
strict private
FValues: array[0..1] of Integer;
function GetValue(Index: Integer): Integer;
procedure SetValue(Index: Integer; const Value: Integer);
public
property Values[Index: Integer]: Integer read GetValue write SetValue;
end;
{ TTest }
function TTest.GetValue(Index: Integer): Integer;
begin
Result := FValues[Index];
end;
procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
FValues[Index] := Value;
end;
我有一个class有多个变量,可以自己访问属性:
TGame = class(TObject)
strict private
FValue1 : Integer;
FValue2 : Integer;
private
procedure SetValue1(const Value : Integer);
procedure SetValue2(const Value : Integer);
function GetValue1() : Integer;
function GetValue2() : Integer;
public
property Value1 : Integer read GetValue1 write SetValue1;
property Value2 : Integer read GetValue2 write SetValue2;
我想知道,是否有办法对不同的属性使用相同的 getter 和 setter,如下所示:
property Value1 : Integer read GetValue write SetValue;
property Value2 : Integer read GetValue write SetValue;
是的,这可以使用 index specifiers:
来实现Index specifiers allow several properties to share the same access method while representing different values.
例如,
type
TTest = class
strict private
FValues: array[0..1] of Integer;
function GetValue(Index: Integer): Integer;
procedure SetValue(Index: Integer; const Value: Integer);
public
property Value1: Integer index 0 read GetValue write SetValue;
property Value2: Integer index 1 read GetValue write SetValue;
end;
{ TTest }
function TTest.GetValue(Index: Integer): Integer;
begin
Result := FValues[Index];
end;
procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
FValues[Index] := Value;
end;
当然,这也适用于您原来的私有字段:
type
TTest = class
strict private
FValue1: Integer;
FValue2: Integer;
function GetValue(Index: Integer): Integer;
procedure SetValue(Index: Integer; const Value: Integer);
public
property Value1: Integer index 1 read GetValue write SetValue;
property Value2: Integer index 2 read GetValue write SetValue;
end;
{ TTest }
function TTest.GetValue(Index: Integer): Integer;
begin
case Index of
1:
Result := FValue1;
2:
Result := FValue2;
else
raise Exception.Create('Invalid index.');
end;
end;
procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
case Index of
1:
FValue1 := Value;
2:
FValue2 := Value;
end;
end;
但你似乎更愿意需要 array property:
type
TTest = class
strict private
FValues: array[0..1] of Integer;
function GetValue(Index: Integer): Integer;
procedure SetValue(Index: Integer; const Value: Integer);
public
property Values[Index: Integer]: Integer read GetValue write SetValue;
end;
{ TTest }
function TTest.GetValue(Index: Integer): Integer;
begin
Result := FValues[Index];
end;
procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
FValues[Index] := Value;
end;