ジェネリックスのクラス変数
ジェネリックス:インデックス への移動
ジェネリック型で定義されたクラス変数は、型パラメータが識別する各インスタンス化型でインスタンス化されます。
次のコードは、TFoo<Integer>.FCount と TFoo<String>.FCount が 1 回のみインスタンス化されること、これらは 2 つの異なる変数であることを示しています。
{$APPTYPE CONSOLE}
type
TFoo<T> = class
class var FCount: Integer;
constructor Create;
end;
constructor TFoo<T>.Create;
begin
inherited Create;
Inc(FCount);
end;
procedure Test;
var
FI: TFoo<Integer>;
begin
FI := TFoo<Integer>.Create;
FI.Free;
end;
var
FI: TFoo<Integer>;
FS: TFoo<String>;
begin
FI := TFoo<Integer>.Create;
FI.Free;
FS := TFoo<String>.Create;
FS.Free;
Test;
Writeln(TFoo<Integer>.FCount); // outputs 2
Writeln(TFoo<String>.FCount); // outputs 1
end.