Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

delphi - Adding some common methods to different classes

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...