class - 如何在类声明中使用预定义变量

标签 class delphi pascal

有没有办法在类中使用预定义变量或使用起始值定义它? 我的代码:

TRoom = class(TObject)
   private
    pos: array[0..2] of integer;
    up: TRoom;
    right: TRoom;
    down: TRoom;
    left: TRoom;
--> freeSlots: array[0..3] of string = ('up','right','down','left'); <--
    content: string;
   end;

最佳答案

Is there a way to use a predefined variable inside a class or define it with a start value?

不可以,您不能为类的实例成员字段声明初始值。类默认初始化(即为零)。如果您想分配初始值,那么您应该在构造函数中执行此操作。

关于class - 如何在类声明中使用预定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70065136/

相关文章:

Delphi 形式的 Java applet

delphi - Delphi/Pascal 的正确结构语法 if then begin end and ;

math - 如何绘制一个物体并在倾斜正面投影中旋转它

pointers - 如果在 P 设置为 nil 后将 C 类的字段 F 分配给指针 P,在 FreePascal 中会发生什么?

python - 在Python中,使用元类来添加和删除许多类属性是否可以接受?

c++ - 将 const char* 赋值给 char 时类型不兼容

delphi - Delphi的条件编译允许定义的符号包含值吗?

delphi - 将时间添加到 TDateTime

c# - 转到实现的方法而不是接口(interface)

c++ - 以下代码如何工作(从 DLL 加载动态类)?