Tôi muốn viết TCheckBox
và TRadioButton
con cháu có 3 phương pháp giống nhau.Làm cách nào để triển khai các phương thức giống hệt nhau với từ 2 Lớp trở lên?
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;
Tôi làm cách nào để thực hiện việc này?
Bạn có nghĩa là khai báo giống hệt nhau (sử dụng Giao diện) hoặc triển khai giống hệt nhau (sử dụng cùng tổ tiên)? – Kromster
@Krom, Đó là một câu hỏi hay. Những gì tôi thực sự có nghĩa là thực hiện giống hệt nhau. – ZigiZ
Bạn phải cho chúng tôi biết cách triển khai để có được câu trả lời hợp lệ. –