Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following structure... Three controls made by me, that inherit from the same common control TCustomControl.

TDriveBar [MyShell.pas] > TCustomPanel [Vcl.ExtCtrls.pas] > TCustomControl [Vcl.Controls.pas]...

TPathBar [MyShell.pas] > TCustomControl [Vcl.Controls.pas]...

TFileList [MyShell.pas] > TCustomSuperList [MySuperList.pas] > TCustomControl [Vcl.Controls.pas]...

Now, I would like to change something so that my TDriveBar, TPathBar and TFileList can be accessed (typecast) as a common ancestor class and have some new methods in common. That ancestor seems to be TCustomControl, but I don't know if it's possible to insert a new class there. Even more, I would prefer that TCustomSuperList not to contain those new methods when it's used alone. I don't know if I made myself understood...

I cannot use interfaces because the idea is to write the common methods only once and use them in those thre controls... With an interface, I have to implement it in every Shell control...

question from:https://stackoverflow.com/questions/65946949/adding-some-common-methods-to-different-classes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
322 views
Welcome To Ask or Share your Answers For Others

1 Answer

Delphi has the ability to delegate the methods of an interface to another object using the keyword implements.

In your case, the idea is to create an object implementing an interface with the common methods and in each of your components you create a property having the interface type and delegated to the object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...