有一个 TPerson 类。众所周知,FSecondName 对于每个对象都是唯一的。
type
TPerson = class(TObject)
private
FAge: Integer;
FFirstName: String;
FSecondName: String;
public
property Age: Integer read FAge;
property FirstName: String read FFirstName;
property SecondName: String read FSecondName;
constructor Create;
end;
如何添加类字段(如 C# 中的静态字段)Persons: TDictionary (String, TPerson),其中键是 SecondName,值是类 TPerson 的对象。
谢谢!
最佳答案
您可以声明一个类变量:
type
TMyClass = class
private
class var
FMyClassVar: Integer;
end;
显然,您可以使用您喜欢的任何类型作为类变量。
类变量具有全局存储。因此该变量只有一个实例。 Delphi 类变量直接类似于 C# 静态字段。
关于delphi - Delphi中的类字段(静态字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18145204/