将子接口作为其父接口参数实现的传递对象

问题描述 投票:0回答:1

我有一个父接口(IParent),一个子接口(IChild)和一个实现子接口的对象。

我正在尝试通过传递实现子接口的对象数组来调用接受array of IParent参数的函数。

编译时出现以下错误:

[[dcc32错误] Unit1.pas(46):E2010不兼容的类型:'IParent'和'TForm1'

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  IParent = interface
    procedure DoSomething();
  end;

  IChild = interface(IParent)
    procedure DoSomethingElse();
  end;

  TForm1 = class(TForm, IChild)
    procedure FormCreate(Sender: TObject);
  public
    procedure DoSomething();
    procedure DoSomethingElse();
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure CallAllDoSomething(AArray : array of IParent);
var
  i : integer;
begin
  i := 0;
  while(i < Length(AArray)) do
  begin
    AArray[i].DoSomething();
    Inc(i);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Unit1.CallAllDoSomething([Self]);
end;

procedure TForm1.DoSomething();
begin
  ShowMessage('Something');
end;

procedure TForm1.DoSomethingElse();
begin
  ShowMessage('Something else');
end;

end.
delphi interface delphi-xe7
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.