Delphi - inherit from a class and an interface (adapter pattern)?

社会主义新天地 提交于 2019-12-05 12:53:22

No that it not correct. You can add an interface to any class you like as follows:

type
  IAdapter = interface
    procedure DoSomething;
  end;

  TAdapter = class(TBaseClass, IInterface, IAdapter)
  private
    FRefCount: Integer;
    procedure DoSomething;
  protected
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  end;

function TAdapter.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

function TAdapter._AddRef: Integer;
begin
  Result := InterlockedIncrement(FRefCount);
end;

function TAdapter._Release: Integer;
begin
  Result := InterlockedDecrement(FRefCount);
  if Result = 0 then
    Destroy;
end;

procedure TAdapter.DoSomething;
begin
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!