[英]Multiple Type Inheritance in Ada
假设我有以下内容:
type blah is abstract tagged
record
element1 : integer;
end record;
type blah2 is abstract tagged
record
element2 : integer;
end record;
我希望我可以做这样的事情:
type blah3 is abstract new blah1 and blah 2 with null record;
所以理论上我现在可以访问 blah3.element1 和 blah3.element2
这可能吗? 和任何提示或技巧?
更新:
是否可以使用指针引用 blah3(包含 blah 和 blah2)的元素?
IE(这只是一个粗略的想法代码很糟糕......大声笑)
type blah3 is new type with
record
element1 : ptr to blah.element1;
element2 : ptr to blah2.element2;
end record
然后例如可以通过 blah3.element1 访问?
Marc C 是对的(像往常一样)。
即使在支持它的语言中,直接多重继承也非常有争议。 在某些边缘情况下编译器应该做什么存在很大的问题,例如当两个父类定义相同方法或成员的不同版本时。 它明确地不是在Ada95中时,允许他们增加了继承。
所以你的下一个问题将是“那么我该怎么做我想做的事?”
这取决于您试图通过使用多重继承来实现什么。 在最坏(最复杂)的情况下,您通常可以通过“mixin”继承实现您正在寻找的效果。 我以前做过,但我仍然认为它在这篇 AdaIC 文章中得到了最好的解释: Ada95 和多重继承,而不是我自己做的。
这是一个摘要:
Ada 95 支持多继承模块包含(通过多个“with”/“use”子句),通过私有扩展和记录组合的多继承“is-implemented-using”,以及通过使用泛型的多继承混合、正式包和访问判别器。
似乎 Ada 2005 有另一种更简单的方法来做到这一点(“接口”),但我还没有机会尝试。 您可以在此处阅读有关它的更多信息(包括为什么直接 MI 在 Ada 中仍然被认为是不好的)。 我找到了这个例子。 同样,这仅在您的编译器支持 Ada 2005 时才有效
Interfaces can be composed from other interfaces thus
type Int2 is interface;
...
type Int3 is interface and Int1;
...
type Int4 is interface and Int1 and Int2;
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.