2

2つのコンポーネントAとBがあります。コンポーネントBはコンポーネントAから派生し、ほとんどのプロパティと手順をコンポーネントAと共有しています。今、私はこのような長い手順を持っています:

procedure DoSomething;
begin
  Form1.Caption := Component_A.Caption;
  // hundreds of additional lines of code calling component A
end;

コンポーネントBがアクティブであるかどうかに応じて、上記の手順を再利用し、Component_AパーツをコンポーネントBの名前に置き換えます。次のようになります。

procedure DoSomething;
var
  C: TheComponentThatIsActive;
begin
  if Component_A.Active then
    C := Component_A;
  if Component_B.Active then
    C := Component_B;
  Form1.Caption := C.Caption;
end;

Delphi2007でそれを行うにはどうすればよいですか?

ありがとう!

4

2 に答える 2

4

TheComponentThatIsActiveComponentA( )と同じタイプである必要がありますTComponentA

ここで、一部のプロパティ/メソッドのみが属する障害に遭遇した場合はComponentB、それを確認して型キャストします。

procedure DoSomething;
var
    C: TComponentA;

begin
    if Component_A.Active then
        C := Component_A
    else if Component_B.Active then
        C := Component_B
    else
        raise EShouldNotReachHere.Create();

    Form1.Caption := C.Caption;

    if C=Component_B then
        Component_B.B_Only_Method;
end;
于 2011-05-27T17:38:23.623 に答える
2

ComponentAまたはComponentBをパラメーターとしてDoSomethingに渡すことができます。

ComponentA = class
public 
 procedure Fuu();
 procedure Aqq();
end;

ComponentB = class(ComponentA)
public 
 procedure Blee();
end;

implementation

procedure DoSomething(context:ComponentA);
begin
  context.Fuu();
  context.Aqq();
end;

procedure TForm1.Button1Click(Sender: TObject);
var cA:ComponentA;
    cB:ComponentB;
begin
  cA:= ComponentA.Create();
  cB:= ComponentB.Create();

  DoSomething(cA);
  DoSomething(cB);

  cA.Free;
  cB.Free;
end;
于 2011-05-27T18:20:57.137 に答える