我想知道如何在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;
调用是否有误?