为什么在一种情况下需要 static 而在另一种情况下不是强制性的?
Why static is needed in the one case and is not mandatory in another one?
FreePascal,Delphi 模式。经过一些实验,我发现这段代码是编译的,FPC 告诉我 class 程序必须是 "static"。但我的问题是:那为什么 operator Equal
不需要 "static" 并且编译正常?!我也不明白 "class" 过程和 "class" 与 "static" 有什么区别(例如,在 classmethod
中的 Python 中你得到参数 - 参考到 class,在 staticmethod
- 你没有这样的论点)。
type TPos = record
FLine: Word;
FPos: Word;
class procedure Init(out a: TPos); static;
class operator Equal(a, b: TPos): Boolean;
end;
PS。我设置 "delphi" 标签是因为:1) 它是以 delphi 模式编写的 2) 因为我发现 Delphi 的相同文档:关于 class 和静态关键字。
与您所说的相反,非静态 class 方法确实有对作为参数传递的 class 的引用。这是一个名为 Self
.
的隐式参数
对于与 classes 不同的记录上的 class 方法,由于没有继承,此 Self
参数将毫无用处,因此它永远不会被传递。因此,记录上的所有 class 方法都必须是静态的。
A class 运算符隐式是静态方法,因此您无需声明。换句话说,operator
意味着 static
。
FreePascal,Delphi 模式。经过一些实验,我发现这段代码是编译的,FPC 告诉我 class 程序必须是 "static"。但我的问题是:那为什么 operator Equal
不需要 "static" 并且编译正常?!我也不明白 "class" 过程和 "class" 与 "static" 有什么区别(例如,在 classmethod
中的 Python 中你得到参数 - 参考到 class,在 staticmethod
- 你没有这样的论点)。
type TPos = record
FLine: Word;
FPos: Word;
class procedure Init(out a: TPos); static;
class operator Equal(a, b: TPos): Boolean;
end;
PS。我设置 "delphi" 标签是因为:1) 它是以 delphi 模式编写的 2) 因为我发现 Delphi 的相同文档:关于 class 和静态关键字。
与您所说的相反,非静态 class 方法确实有对作为参数传递的 class 的引用。这是一个名为 Self
.
对于与 classes 不同的记录上的 class 方法,由于没有继承,此 Self
参数将毫无用处,因此它永远不会被传递。因此,记录上的所有 class 方法都必须是静态的。
A class 运算符隐式是静态方法,因此您无需声明。换句话说,operator
意味着 static
。