Ada中的多类型继承

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

假设我有以下内容:

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)?

即(这只是一个粗糙的想法,代码太糟糕了……大声笑)

type blah3 is new type with
record
element1 : ptr to blah.element1;
element2 : ptr to blah2.element2;
end record

然后可以通过例如blah3.element1?

types polymorphism multiple-inheritance ada
1个回答
4
投票

Marc C是正确的(照常)。

直接多重继承即使在支持它的语言中也很有争议。在某些极端情况下,例如当两个父类都定义同一方法或成员的不同版本时,编译器应该做什么会有很大的问题。当他们添加继承时,在Ada95中明确允许not

因此,您的下一个问题将是“那么我该怎么做?”

这取决于您尝试通过使用多重继承来实现的目标。在最坏(最复杂)的情况下,通常可以通过“ mixin”继承来达到所需的效果。我以前曾经做过,但是我仍然认为这在AdaIC文章中得到了最好的解释:Ada95 and Multiple Inheritance比我本人可以做的。

这里是摘要:

Ada 95支持多重继承模块包含(通过多个“ with” /“ use”子句),多重继承通过私有“实现使用”扩展名和记录组成,以及通过多个继承使用泛型,正式软件包,以及访问判别式。

看来Ada 2005还有另一种更简单的方法(“接口”),但是我还没有机会尝试过。您可以阅读有关它的更多信息(包括为什么直接MI在Ada中<< still >>被认为是错误的)here。我找到了这个例子。同样,这仅在编译器支持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; ...

© www.soinside.com 2019 - 2024. All rights reserved.