Multiple Inheritance for Interfaces

Is there a way to define an interface that inherits from two or more interfaces?

I tried the following code on Delphi2007:

  IInterfaceA = interface
     procedure A;
  end;

  IInterfaceB = interface
    procedure B;
  end;

  IInterfaceAB = interface(IInterfaceA, IInterfaceB);

.. and this caused error E2029:

E2029 ')' expected, but ',' found

on the line:

IInterfaceAB = interface(IInterfaceA, IInterfaceB).

+4
source share
3 answers

There is no multiple interface inheritance in Delphi, since there is no multiple inheritance in Delphi. All you can do is make a class that implements several interfaces at once.

TMyClass = class(TInterfacedObject, IInterfaceA, IInterfaceB)
+7
source

Delphi , . ( ISP-), , .

+6

IMHO, in this case you must define three types. One for the interface and the third for multiple inheritance:

  IInterfaceA = interface
     procedure A;
  end;

  IInterfaceB = interface
    procedure B;
  end;

  TiA = class(TInterfacedObject, IInterfaceA)
    procedure A;
  end;

  TiB = class(TInterfacedObject, IInterfaceB)
    procedure B;
  end;

  TMyObject = class(TInterfacedObject, IInterfaceA, IInterfaceB)
  private
    _iA : IInterfaceA;
    _iB : IInterfaceB;
    function getiA : IInterfaceA;
    function getiB : IInterfaceB;
  public
    property iA : IInterfaceA read getiA implements IInterfaceA;
    property iB : IInterfaceB read getiB implements IInterfaceB;
  end;
{.....}
{ TMyObject }

function TMyObject.getiA: IInterfaceA;
begin
  if not Assigned(_iA) then _iA := TIA.Create;
  Result := _iA;
end;

function TMyObject.getiB: IInterfaceB;
begin
  if not Assigned(_iB) then _iB := TIB.Create;
  Result := _iB;
end;
+1
source

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


All Articles