避免 Delphi 中的代码重复

发布于 2024-11-10 00:10:54 字数 562 浏览 0 评论 0原文

我有两个组件 A 和 B。组件 B 派生自组件 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中呢?

谢谢!

I have two components A and B. Component B derives from component A and shares most properties and procedures with it. Now I have a lengthy procedure like this:

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

Depending on whether component B is active or not, I would like to reuse the above procedure and replace the Component_A part with the name of component B. It should look like this then:

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;

How can I do that in Delphi2007?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-11-17 00:10:54

TheComponentThatIsActive 的类型应与 ComponentA 的类型 (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;

TheComponentThatIsActive should be the same type that ComponentA is (TComponentA).

Now, if you run into a stumbling block where some properties/methods only belong to ComponentB, then check and typecast it.

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;
硬不硬你别怂 2024-11-17 00:10:54

您可以将 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;

You can pass ComponentA or ComponentB to DoSomething as a parameter.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文