为什么此代码有效?Delphi 在这种情况下如何实例化 class?

Why is this code working and how is Delphi instantiating a class in this case?

我正在练习 Delphi 轨道,并遵循 Delphi 如何为表单生成代码,回答了如下基本问题之一:

unit uLeap;

interface

type
  TSYear = class
    public
      { public declarations here }
      function isLeap(y: integer): boolean;
  end;

var
TYear: TSYear;

implementation

function TSYear.isLeap(y: integer): boolean;
begin
  result := ((y mod 4) = 0) and (((y mod 400) = 0) or ((y mod 100) <> 0));
end;

end.

代码编译没有任何抱怨,我可以 运行 一步一步,并且“isLeap”函数被另一个单元这样调用了几次:

procedure YearTest.year_divisible_by_4_not_divisible_by_100_leap_year;
begin
  assert.IsTrue(TYear.IsLeap(1996), 'Expected ''true'', 1996 is a leap year.');
end;
...

我从来没有明确地创建过 class 的实例,但似乎 Delphi 正在某处创建它,也许是在声明 TYear 时?这是一个有效的方法吗?

尽管通过了所有测试,代码还是被拒绝了,因为它不是以传统方式完成的。我最终肯定会以不同的方式来接受它,但是,除了糟糕的命名之外,为什么这样行得通?这段代码会不会在我在这个简单示例中看不到的地方引起问题?

I've never explicitly created the instance of the class, but it seems as if Delphi is doing it somewhere, maybe when declaring TYear?

不,Delphi 不会自动创建您的实例。当您声明一个 class 类型的变量时,它只是一个可以指向有效实例的指针变量。但是你必须始终自己创建这个实例,并将指针保存在变量中:

SYear := TSYear.Create; // create a `TSYear` object and save its address in `SYear`

Is that a valid way?

没有

[W]hy is this working?

因为你很幸运:isLeap 函数不访问 class 实例上的任何字段。

Would this code cause problems somewhere I can't see in this simple example?

如果函数一直在使用 class 实例中的任何字段,幸运的话你会得到一个 AV,如果不幸的话会导致内存损坏。

解决方案是创建一个实例并使用它:

SYear := TSYear.Create;
try
  ShowMessage(BoolToStr(SYear.IsLeap(2000), True));
finally
  SYear.Free;
end;

或者,由于您显然不需要任何实例变量来确定年份是否为闰年,因此最好将其设为 class method:

type
  TSYear = class
  public
    class function IsLeap(AYear: Integer): Boolean; static;
  end;

这样,可以在没有任何 class 实例的情况下调用它:TSYear.IsLeap(2000)。请注意,TSYear 是 class(类型)名称,而不是此类型的变量。

请参阅 documentation 以获得对所有这些概念的精彩概念性介绍。