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

前端之家收集整理的这篇文章主要介绍了Delphi中的类字段(静态字段)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个类TPerson.众所周知,FSecondName对每个对象都是唯一的.
  1. type
  2. TPerson = class(TObject)
  3. private
  4. FAge: Integer;
  5. FFirstName: String;
  6. FSecondName: String;
  7. public
  8. property Age: Integer read FAge;
  9. property FirstName: String read FFirstName;
  10. property SecondName: String read FSecondName;
  11. constructor Create;
  12. end;

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

谢谢!

解决方法

您可以声明一个类变量:
  1. type
  2. TMyClass = class
  3. private
  4. class var
  5. FMyClassVar: Integer;
  6. end;

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

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

猜你在找的Delphi相关文章