当一个类实现了后代接口时,为什么它不自动算作实现了基接口?
这无法编译的原因是什么?
type
IInterfaceA = interface ['{44F93616-0161-4912-9D63-3E8AA140CA0D}']
procedure DoA;
end;
IInterfaceB = interface(IInterfaceA) ['{80CB6D35-E12F-462A-AAA9-E7C0F6FE0982}']
procedure DoB;
end;
TImplementsAB = class(TSingletonImplementation, IInterfaceB)
procedure DoA;
procedure DoB;
end;
var
ImplementsAB: TImplementsAB;
InterfaceA: IInterfaceA;
InterfaceB: IInterfaceB;
begin
ImplementsAB := TImplementsAB.Create;
InterfaceA := ImplementsAB; >> incompatible types
...
end
相反,这就是我让它工作的方式:
InterfaceA := ImplementsAB as InterfaceB;
或者
InterfaceA := InterfaceB;
我的意思是,如果 IInterfaceB 继承自 IInterfaceA 并且 TImplementsAB 实现 IInterfaceB,那么也实现 IInterfaceA 并保持类型兼容是不合逻辑的吗?
What's the reason this won't compile?
type
IInterfaceA = interface ['{44F93616-0161-4912-9D63-3E8AA140CA0D}']
procedure DoA;
end;
IInterfaceB = interface(IInterfaceA) ['{80CB6D35-E12F-462A-AAA9-E7C0F6FE0982}']
procedure DoB;
end;
TImplementsAB = class(TSingletonImplementation, IInterfaceB)
procedure DoA;
procedure DoB;
end;
var
ImplementsAB: TImplementsAB;
InterfaceA: IInterfaceA;
InterfaceB: IInterfaceB;
begin
ImplementsAB := TImplementsAB.Create;
InterfaceA := ImplementsAB; >> incompatible types
...
end
In contrast this is how I make it work:
InterfaceA := ImplementsAB as InterfaceB;
or
InterfaceA := InterfaceB;
I mean, if IInterfaceB inherits from IInterfaceA and TImplementsAB implements IInterfaceB, it wouldn't be logical to also implement IInterfaceA and be type compatible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为早期的 OLE/COM 有一个错误,Borland 决定与其兼容。本文提到了这一点:新的 Delphi 语言功能:Delphi for .NET 中接口的多重继承 。解决方案是在类中显式列出所有祖先接口,如 Mikael 所写。
链接文章中的一些引用:
This so because early OLE/COM had a bug and Borland decided to be compatible with it. This is mentioned in this article: New Delphi language feature: Multiple inheritance for interfaces in Delphi for .NET. The solution is to list all ancestor interfaces explicitly in the class as Mikael wrote.
Some quotes from the linked article:
使其工作的另一种方法是将两个接口都包含在类声明中。
我想这就是编译器认识到
TImplementsAB
实现IInterfaceA
和IInterfaceB
所需要的。Another way to make it work is to include both interfaces in the class declaration.
I guess this is what is required for the compiler to realize that
TImplementsAB
implements bothIInterfaceA
andIInterfaceB
.