Can I create a class that inherits from the class and from interfaces in Delphi?

I have a TDevice class. Some devices will have a cellular module. Therefore, I am creating an IIMEI interface. Other devices will have an Ethernet module. Therefore, I am creating an IMacAddress interface.

So, I would like to create another class that is a child of TDevice and implements IIMEI or IMacAddress or both.

Is this possible in Delphi?

+4
source share
1 answer

The easiest option is to derive the TDevice from the TInterfaced Object and simply extend your descendants with additional methods. Beware of counting links to the interface, although otherwise you will have many unexpected access violations.

Alternatively, you can write a wrapper object that descends from TInterfacedObject and delegates the implementation of interfaces to TDevice descendants. In this case, link counting will be less problematic.

TMacAddressWrapper = class(TInterfacedObject, IMacAddress) private FDevice: TDevice; property Device: TDevice read FDevice implements IMacAddress; public constructor Create(_Device: TDevice); end; constructor TMacAddressWrapper.Create(_Device: TDevice); begin inherited Create; FDevice := _Device; end; 
+3
source

Source: https://habr.com/ru/post/1286694/


All Articles