从 Ada 中的两种类型继承

发布于 2024-12-27 08:42:33 字数 1181 浏览 1 评论 0原文

假设我有:

GENERIC
   TYPE Item IS PRIVATE;
   PACKAGE Abstract_something IS
      TYPE something IS ABSTRACT TAGGED LIMITED PRIVATE;
      procedure x(...)IS ABSTRACT;
      procedure y(...)IS ABSTRACT;
      PRIVATE
         TYPE something IS ABSTRACT TAGGED LIMITED NULL RECORD;
END Abstract_something;

然后我创建了两个孩子

1.

GENERIC
   PACKAGE Abstract_something.Child IS
      TYPE something_2  IS ABSTRACT NEW something WITH PRIVATE;
      PROCEDURE x(...);
      PROCEDURE y(...);
      FUNCTION  xx(...) RETURN whatever1;
      error: EXCEPTION;
      PRIVATE
         TYPE something_2  IS ABSTRACT NEW something WITH RECORD
            some declarations here..
         END RECORD;
END Abstract_something.Child;

2.

GENERIC
PACKAGE Abstract_something.Child2 IS
   TYPE something3 IS ABSTRACT NEW something WITH PRIVATE;
   PROCEDURE z ( ... ) IS ABSTRACT;
   PRIVATE
      TYPE something3 IS ABSTRACT NEW something WITH NULL RECORD;
END Abstract_something.Child2;

这里 child 和 child2 都继承自同一个父级,我想创建 child3 其类型为something4,与something2相同,并向其中添加过程Z 某事3。 可以吗?又如何呢?

谢谢。

suppose i have:

GENERIC
   TYPE Item IS PRIVATE;
   PACKAGE Abstract_something IS
      TYPE something IS ABSTRACT TAGGED LIMITED PRIVATE;
      procedure x(...)IS ABSTRACT;
      procedure y(...)IS ABSTRACT;
      PRIVATE
         TYPE something IS ABSTRACT TAGGED LIMITED NULL RECORD;
END Abstract_something;

then i make two children

1.

GENERIC
   PACKAGE Abstract_something.Child IS
      TYPE something_2  IS ABSTRACT NEW something WITH PRIVATE;
      PROCEDURE x(...);
      PROCEDURE y(...);
      FUNCTION  xx(...) RETURN whatever1;
      error: EXCEPTION;
      PRIVATE
         TYPE something_2  IS ABSTRACT NEW something WITH RECORD
            some declarations here..
         END RECORD;
END Abstract_something.Child;

2.

GENERIC
PACKAGE Abstract_something.Child2 IS
   TYPE something3 IS ABSTRACT NEW something WITH PRIVATE;
   PROCEDURE z ( ... ) IS ABSTRACT;
   PRIVATE
      TYPE something3 IS ABSTRACT NEW something WITH NULL RECORD;
END Abstract_something.Child2;

here both child and child2 inherit from same parent and i want to create child3
that has type something4 that is identical to something2 and adds to it procedure Z from
something3.
can it be done? and how?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假情假意假温柔 2025-01-03 08:42:33

Ada 不支持多重继承,Ada 2005 中的“接口”除外,但它没有关联的类型。根据您所写的内容,您可以通过创建 Something3 的接口来使用该概念(无论如何它都有一个空记录,即使您没有在示例中公开它)。

Something3真的需要继承Abstract_something吗?如果没有,你可以这样做

type something3 is interface;
type something4 is new something2 and something3 with private; -- or with null record etc

根据我的经验,仔细考虑手头问题的真正属性(而不是实现的属性)是值得的:在继承的情况下,是否存在“is-a”关系存在于整个层次结构中吗?也就是说,当B和C继承自A,并且D同时继承自B和C时,是否每个B和C也是A?每个D真的都是B和C吗?
“Has-a”不适合继承(尽管粗心的人可能会那样实现)。

Ada does not support multiple inheritance, except for "Interfaces" in Ada 2005, which however do not have an associated type. From what you've written, you could use that concept by making an interface of something3 (it has a null record anyway, even if you do not expose that in your example).

Does something3 really need to inherit from Abstract_something? If not, you could do

type something3 is interface;
type something4 is new something2 and something3 with private; -- or with null record etc

From my experience, it pays to think carefully about what the properties of the problem at hand really are (as opposed to those of the implementation): in the case of inheritance, does an "is-a" relationship exist throughout the hierarchy? That is, when B and C inherit from A, and D inherits from both B and C, is every B and C also an A? Is every D really both a B and a C?
"Has-a" does not lend itself to inheritance (although the unwary may implement it that way).

坠似风落 2025-01-03 08:42:33

您可以使用正交继承,它可能适用于您的用例。您必须将 Child2 更改为:

GENERIC
   TYPE base IS ABSTRACT NEW something WITH PRIVATE;
PACKAGE Abstract_something.Child2 IS
   TYPE something3 IS ABSTRACT NEW base AND something3_interface WITH PRIVATE;
   PROCEDURE z ( ... ) IS ABSTRACT;
   PRIVATE
      TYPE something3 IS ABSTRACT NEW base AND something3_interface WITH NULL RECORD;
END Abstract_something.Child2;

现在 something3 中的添加可以应用于继承自 something 的任何类型 - 您只需实例化 Abstract_something.Child2 与您想要继承的base - 例如something_2

something3_interface 是可选的,必须添加到 Abstract_something 中:

...
TYPE something3_interface IS INTERFACE;
-- Possibly some primitive operations defined by something3_interface here
...

仅当您想使用 something3 类型时,才需要接口 something3_interface 某处未指定 Abstract_something.Child2 的实例化 - 由于其通用包有自己的参数,因此您不能直接使用 something3Abstract_something.Child2 之外。

You can use orthogonal inheritance, it might apply to your use case. You'd have to change Child2 to:

GENERIC
   TYPE base IS ABSTRACT NEW something WITH PRIVATE;
PACKAGE Abstract_something.Child2 IS
   TYPE something3 IS ABSTRACT NEW base AND something3_interface WITH PRIVATE;
   PROCEDURE z ( ... ) IS ABSTRACT;
   PRIVATE
      TYPE something3 IS ABSTRACT NEW base AND something3_interface WITH NULL RECORD;
END Abstract_something.Child2;

Now the additions in something3 can be applied to any type that inherits from something - you just have to instantiate Abstract_something.Child2 with the base you want to inherit from - like e.g. something_2.

something3_interface is optional and would have to be added to Abstract_something:

...
TYPE something3_interface IS INTERFACE;
-- Possibly some primitive operations defined by something3_interface here
...

You need the interface something3_interface only if you want to use the type something3 somewhere without specifying an instantiation of Abstract_something.Child2 - as its generic package has an own parameter, you cannot use something3 directly outside of Abstract_something.Child2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文