如何用 2 个或更多类实现相同的方法?
我想编写一个具有 3 个相同方法的 TCheckBox
和 TRadioButton
后代。
TMyCheckBox = class(TCheckBox)
procedure DoSomething1;
procedure DoSomething2;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
TMyRadioButton = class(TRadioButton)
procedure DoSomething1;
procedure DoSomething2;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
// the following procedures are common for both classes, so in fact
// TMyCheckBox.DoSomething1 do the same as TMyRadioButton.DoSomething1
procedure DoSomething1;
begin
// here is the same code for TMyCheckBox as well as for TMyRadioButton
// but I don't want to write the same code many times but implement it
// for both classes at once in some common way
end;
procedure DoSomething2;
begin
// here is the same code for TMyCheckBox as well as for TMyRadioButton
// but I don't want to write the same code many times but implement it
// for both classes at once in some common way
end;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
begin
// here is the same code for TMyCheckBox as well as for TMyRadioButton
// but I don't want to write the same code many times but implement it
// for both classes at once in some common way
end;
我该怎么做?
I want to write a TCheckBox
and TRadioButton
descendants having 3 identical methods.
TMyCheckBox = class(TCheckBox)
procedure DoSomething1;
procedure DoSomething2;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
TMyRadioButton = class(TRadioButton)
procedure DoSomething1;
procedure DoSomething2;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
// the following procedures are common for both classes, so in fact
// TMyCheckBox.DoSomething1 do the same as TMyRadioButton.DoSomething1
procedure DoSomething1;
begin
// here is the same code for TMyCheckBox as well as for TMyRadioButton
// but I don't want to write the same code many times but implement it
// for both classes at once in some common way
end;
procedure DoSomething2;
begin
// here is the same code for TMyCheckBox as well as for TMyRadioButton
// but I don't want to write the same code many times but implement it
// for both classes at once in some common way
end;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
begin
// here is the same code for TMyCheckBox as well as for TMyRadioButton
// but I don't want to write the same code many times but implement it
// for both classes at once in some common way
end;
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用三个方法签名定义一个接口,例如
IDoSomething
。然后将您的类声明更改为
然后实施。
如果实现很常见或非常接近。
然后定义一个辅助类
TDoSomething
并委派工作。例如
delphi中的类方法,相当于其他语言中的静态方法。
Define an interface say
IDoSomething
with the the three method signatures.Then change your class declaration to
and then implement.
If the implementations are common or very close.
Then define a helper class
TDoSomething
and then delegate the work.e.g.
Class Methods in delphi, equivalent to static methods in other languages.
您正在寻找实现继承而不是接口继承。只有当您可以从单个共同祖先派生类时,这才可以在 Delphi 中实现。此限制是固有的,因为该语言仅支持单继承。
您能做的最好的事情是这样的:
对于
TMyRadioButton
等也是如此。现在,您可以使用接口和委托来减少一些样板文件,但没有办法帮助消息处理程序就像
WMSize
。You are looking for implementation inheritance rather than interface inheritance. This is only achievable in Delphi if you can derive classes from a single common ancestor. This limitation is inherent because the language only supports single-inheritance.
The best you can do is something like this:
And likewise for
TMyRadioButton
etc.Now, you could use interfaces and delegation to reduce some of the boilerplate, but there's no way for that to help with a message handler like
WMSize
.