避免 Delphi 中的代码重复
我有两个组件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TheComponentThatIsActive
的类型应与ComponentA
的类型 (TComponentA
) 相同。现在,如果您遇到某些属性/方法仅属于 ComponentB 的绊脚石,请检查并对其进行类型转换。
TheComponentThatIsActive
should be the same type thatComponentA
is (TComponentA
).Now, if you run into a stumbling block where some properties/methods only belong to
ComponentB
, then check and typecast it.您可以将 ComponentA 或 ComponentB 作为参数传递给 DoSomething。
You can pass ComponentA or ComponentB to DoSomething as a parameter.