相关疑难解决方法(0)

Delphi接口继承:为什么我不能访问祖先接口的成员?

假设您有以下内容:

//Note the original example I posted didn't reproduce the problem so
//I created an clean example  
  type
    IParent = interface(IInterface)
    ['{85A340FA-D5E5-4F37-ABDD-A75A7B3B494C}']
      procedure DoSomething;
    end;

    IChild = interface(IParent)
    ['{15927C56-8CDA-4122-8ECB-920948027015}']
      procedure DoSomethingElse;
    end;

    TGrandParent = class(TInterfacedObject)
    end;

    TParent = class(TGrandParent)
    end;

    TChild = class(TParent, IChild)
    private
      FChildDelegate: IChild;
    public
      property ChildDelegate:IChild read FChildDelegate implements IChild;
    end;

    TChildDelegate = class(TInterfacedObject, IChild)
    public
      procedure DoSomething;
      procedure DoSomethingElse;
    end;
Run Code Online (Sandbox Code Playgroud)

我认为这可以让你打电话,DoSomething但似乎并非如此:

procedure CallDoSomething(Parent: TParent);
begin
  if Parent is TChild then
    TChild(Parent).DoSomething;
end;
Run Code Online (Sandbox Code Playgroud)

很明显,编译器正在强制执行接口继承,因为除非实现了成员,否则这两个类都不会编译 …

delphi inheritance interface delphi-2009

11
推荐指数
2
解决办法
5590
查看次数

标签 统计

delphi ×1

delphi-2009 ×1

inheritance ×1

interface ×1