简体   繁体   English

Ada 中的多类型继承

[英]Multiple Type Inheritance in Ada

Suppose I have the following:假设我有以下内容:

type blah is abstract tagged 
record 
element1 : integer; 
end record;

type blah2 is abstract tagged
record
element2 : integer;
end record;

I'm hoping that it's possible that I can do something like this:我希望我可以做这样的事情:

type blah3 is abstract new blah1 and blah 2 with null record;

So in theory I can now access blah3.element1 and blah3.element2所以理论上我现在可以访问 blah3.element1 和 blah3.element2

Is this possible?这可能吗? and any hints or tips?和任何提示或技巧?

UPDATE:更新:

Would it be possible to references the elements of blah3 (containing blah and blah2) using pointers?是否可以使用指针引用 blah3(包含 blah 和 blah2)的元素?

IE (this is just a rough idea code is terrible...LOL) IE(这只是一个粗略的想法代码很糟糕......大声笑)

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

and are then able to be accessed via blah3.element1 for example?然后例如可以通过 blah3.element1 访问?

Marc C is right (as usual). Marc C 是对的(像往常一样)。

Direct Multiple inheritance is very controversial even in the languages that support it.即使在支持它的语言中,直接多重继承也非常有争议。 There are big issues about what the compiler is supposed to do in some edge cases, like when both parent classes define different versions of the same method or member.在某些边缘情况下编译器应该做什么存在很大的问题,例如当两个父类定义相同方法或成员的不同版本时。 It was explicitly not allowed in Ada95 when they added inheritance.它明确地不是在Ada95中时,允许他们增加了继承。

So your next question will be "So how do I do what i want to do?"所以你的下一个问题将是“那么我该怎么做我想做的事?”

It depends on what you are trying to achieve by using multiple inheritance.这取决于您试图通过使用多重继承来实现什么。 In the worst (most complicated) case you can usually achieve the effect you are looking for with "mixin" inheritance.在最坏(最复杂)的情况下,您通常可以通过“mixin”继承实现您正在寻找的效果。 I have done it before, but still I think it is explained best in this AdaIC article: Ada95 and Multiple Inheritance than I could do myself.以前做过,但我仍然认为它在这篇 AdaIC 文章中得到了最好的解释: Ada95 和多重继承,而不是我自己做的。

Here's a digest:这是一个摘要:

Ada 95 supports multiple-inheritance module inclusion (via multiple "with"/"use" clauses), multiple-inheritance "is-implemented-using" via private extensions and record composition, and multiple-inheritance mix-ins via the use of generics, formal packages, and access discriminants. Ada 95 支持多继承模块包含(通过多个“with”/“use”子句),通过私有扩展和记录组合的多继承“is-implemented-using”,以及通过使用泛型的多继承混合、正式包和访问判别器。

It appears that Ada 2005 has another easier way to do this ("interfaces"), but I haven't had a chance to try that yet.似乎 Ada 2005 有另一种更简单的方法来做到这一点(“接口”),但我还没有机会尝试。 You can read more about it (including why direct MI is still considered bad in Ada) here .您可以在此处阅读有关它的更多信息(包括为什么直接 MI 在 Ada 中仍然被认为是不好的)。 I found this example.我找到了这个例子。 Again, this will only work if your compiler supports Ada 2005同样,这仅在您的编译器支持 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM