クラスヘルパー

さて久々にDelphiです。

class_helper.dpr

program class_helper;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TFoo = class
  public
    constructor Create;
  end;

  TBar = class helper for TFoo
    procedure Say;
  end;

  THoge = class
  public
    constructor Create;
  end;

  TFuga = class helper for THoge
    procedure Say;
  end;

constructor TFoo.Create;
begin
  WriteLn('Create Foo');
end;

procedure TBar.Say;
begin
  WriteLn('Say Bar');
end;

constructor THoge.Create;
begin
  WriteLn('Create Hoge');
end;

procedure TFuga.Say;
begin
  WriteLn('Say Fuga');
end;

var
  foo: TObject;

begin
  foo := THoge.Create;
  try
    TFoo(foo).Say;
  finally
    foo.Free;
  end;
end.

このコードでちゃんと動くのね。

出力結果

Create Hoge
Say Bar