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

时间:2013-08-09 11:00:51

标签: 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;

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

谢谢!

1 个答案:

答案 0 :(得分:9)

您可以声明一个类变量:

type 
  TMyClass = class
  private
    class var
      FMyClassVar: Integer;
   end;

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

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

相关问题