Delphi 个对象:TObjectList 迭代的快捷方式
Delphi objects: shortcut for TObjectList iteration
我开始学习并在我的项目中积极使用 OOP。我发现像 LDevices.Devices[i]
这样的结构看起来很笨重并且使代码难以阅读。
所以我的问题是:有没有什么方法可以像 LDevices[i]
而不是 LDevices.Devices[i]
那样为字段创建快捷方式来访问它? Devices
是 TObjectList<TDevice>
。
我的对象结构,供参考:
TDeviceStorage = class (TObject)
private
DevicesByID: TDictionary<Integer,TDevice>;
public
Devices: TObjectList<TDevice>;
Constructor Create;
Destructor Destroy; override;
procedure AddDevice(aID: Integer; aName, aShortName: String; aSubtype, aLocation: Integer; aSteamID: String);
procedure Clear();
function Count(): Integer;
function DeviceByID(aID: Integer): TDevice;
function DeviceIndex(aID: Integer): Integer;
end;
是的,这正是 default
directive on an array property 的用途:
TDeviceStorage = class(TObject)
private
FDevicesByID: TDictionary<Integer, TDevice>;
FDevices: TObjectList<TDevice>;
function GetDeviceCount: Integer;
function GetDevice(Index: Integer): TDevice;
procedure SetDevice(Index: Integer; const Value: TDevice);
public
constructor Create;
destructor Destroy; override;
procedure AddDevice(AID: Integer; const AName, AShortName: string;
ASubtype, ALocation: Integer; const ASteamID: string);
procedure Clear;
function DeviceByID(AID: Integer): TDevice;
function DeviceIndex(AID: Integer): Integer;
property Devices[Index: Integer]: TDevice read GetDevice write SetDevice; default;
property DeviceCount: Integer read GetDeviceCount;
end;
和
function TDeviceStorage.GetDevice(Index: Integer): TDevice;
begin
Result := FDevices[Index];
end;
function TDeviceStorage.GetDeviceCount: Integer;
begin
Result := FDevices.Count;
end;
procedure TDeviceStorage.SetDevice(Index: Integer; const Value: TDevice);
begin
FDevices[Index] := Value;
end;
现在,如果DS: TDeviceStorage
,您可以写DS[i]
来访问FDevices
中的第i
个设备。
我开始学习并在我的项目中积极使用 OOP。我发现像 LDevices.Devices[i]
这样的结构看起来很笨重并且使代码难以阅读。
所以我的问题是:有没有什么方法可以像 LDevices[i]
而不是 LDevices.Devices[i]
那样为字段创建快捷方式来访问它? Devices
是 TObjectList<TDevice>
。
我的对象结构,供参考:
TDeviceStorage = class (TObject)
private
DevicesByID: TDictionary<Integer,TDevice>;
public
Devices: TObjectList<TDevice>;
Constructor Create;
Destructor Destroy; override;
procedure AddDevice(aID: Integer; aName, aShortName: String; aSubtype, aLocation: Integer; aSteamID: String);
procedure Clear();
function Count(): Integer;
function DeviceByID(aID: Integer): TDevice;
function DeviceIndex(aID: Integer): Integer;
end;
是的,这正是 default
directive on an array property 的用途:
TDeviceStorage = class(TObject)
private
FDevicesByID: TDictionary<Integer, TDevice>;
FDevices: TObjectList<TDevice>;
function GetDeviceCount: Integer;
function GetDevice(Index: Integer): TDevice;
procedure SetDevice(Index: Integer; const Value: TDevice);
public
constructor Create;
destructor Destroy; override;
procedure AddDevice(AID: Integer; const AName, AShortName: string;
ASubtype, ALocation: Integer; const ASteamID: string);
procedure Clear;
function DeviceByID(AID: Integer): TDevice;
function DeviceIndex(AID: Integer): Integer;
property Devices[Index: Integer]: TDevice read GetDevice write SetDevice; default;
property DeviceCount: Integer read GetDeviceCount;
end;
和
function TDeviceStorage.GetDevice(Index: Integer): TDevice;
begin
Result := FDevices[Index];
end;
function TDeviceStorage.GetDeviceCount: Integer;
begin
Result := FDevices.Count;
end;
procedure TDeviceStorage.SetDevice(Index: Integer; const Value: TDevice);
begin
FDevices[Index] := Value;
end;
现在,如果DS: TDeviceStorage
,您可以写DS[i]
来访问FDevices
中的第i
个设备。