我想写一个TCheckBox
和TRadioButton
后代有3种相同的方法。如何使用2个或更多类实现相同的方法?
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;
我该怎么做?
你的意思是相同的声明(使用接口)或相同的实现(使用相同的祖先)? – Kromster 2012-01-10 14:12:19
@Krom,这是一个很好的问题。我的意思是相同的实现。 – ZigiZ 2012-01-10 16:44:12
您必须向我们展示实施才能获得有效答案。 – 2012-01-10 16:50:26