简体   繁体   English

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

[英]Class field (static field) in Delphi

There is a class TPerson. 有一个类TPerson。 It is known that FSecondName unique to each object. 众所周知,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;

How can I add a class field (like static field in C#) Persons: TDictionary (String, TPerson), where the key is SecondName and the value is an object of class TPerson. 如何添加类字段(如C#中的静态字段)Persons:TDictionary(String,TPerson),其中键是SecondName,值是类TPerson的对象。

Thanks! 谢谢!

You can declare a class variable: 您可以声明一个类变量:

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

Obviously you can use whatever type you like for the class variable. 显然,你可以使用你喜欢的任何类型的类变量。

Class variables have global storage. 类变量具有全局存储。 So there is a single instance of the variable. 所以变量有一个实例。 A Delphi class variable is directly analagous to a C# static field. Delphi类变量与C#静态字段直接相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM