【发布时间】:2015-02-12 15:09:57
【问题描述】:
我想知道如何从 ADA 中的父类调用重写的方法。让我们考虑以下示例。 Parent 类有一些方法被 Child 类覆盖。 Parent 类(即Prints)中有一个方法调用了它的一些被覆盖的方法。但是不会调用被覆盖的方法!这是一个例子:
--- 父母 ---
package Parents is
type Parent is tagged null record;
procedure Prints(Self: in out Parent);
-- these will be overridden
procedure Print1(Self: in out Parent) is null;
procedure Print2(Self: in out Parent) is null;
end Parents;
...
package body Parents is
procedure Prints(Self: in out Parent) is
begin
Put_Line("Parents.Prints: calling prints...");
Self.Print1;
Self.Print2;
end;
end Parents;
--- 孩子---
With Parents;
package Childs is
type Child is new Parents.Parent with null record;
overriding procedure Print1(Self: in out Child);
overriding procedure Print2(Self: in out Child);
end Childs;
...
package body Childs is
procedure Print1(Self: in out Child) is
begin
Put_Line("Child.Print1 is printing...");
end;
procedure Print2(Self: in out Child) is
begin
Put_Line("Child.Print2 is printing...");
end;
end Childs;
---主要---
procedure Main is
anyprint : access Parents.Parent'Class;
begin
anyprint := new Childs.Child;
anyprint.Prints;
end Main;
问题
我期望看到的是从Child 向Print1 和Print2 发送的调用。但是不会调用被覆盖的方法!有 C++ 背景,这种类型的多态调用对我来说很有意义,但我不知道 Ada 是如何对待它们的?
来自Prints 的调用Self.Print1; 错误吗?
【问题讨论】:
标签: oop polymorphism ada