Delphi中的类字段(静态字段)

And*_*rew 6 delphi static tdictionary

有一个类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;
Run Code Online (Sandbox Code Playgroud)

如何添加类字段(如C#中的静态字段)Persons:TDictionary(String,TPerson),其中键是SecondName,值是类TPerson的对象.

谢谢!

Dav*_*nan 10

您可以声明一个类变量:

type 
  TMyClass = class
  private
    class var
      FMyClassVar: Integer;
   end;
Run Code Online (Sandbox Code Playgroud)

显然,你可以使用你喜欢的任何类型的类变量.

类变量具有全局存储.所以变量有一个实例.Delphi类变量与C#静态字段直接相似.