即使有了这个简单的示例,我仍然无法使动态调度工作。我相信问题在于我如何设置类型和方法,但无法看到哪里!Ada中的动态调度
with Ada.Text_Io;
procedure Simple is
type Animal_T is abstract tagged null record;
type Cow_T is new Animal_T with record
Dairy : Boolean;
end record;
procedure Go_To_Vet (A : in out Cow_T) is
begin
Ada.Text_Io.Put_Line ("Cow");
end Go_To_Vet;
type Cat_T is new Animal_T with record
Fur : Boolean;
end record;
procedure Go_To_Vet (A : in out Cat_T)
is
begin
Ada.Text_Io.Put_Line ("Cat");
end Go_To_Vet;
A_Cat : Cat_T := (Animal_T with Fur => True);
A_Cow : Cow_T := (Animal_T with Dairy => False);
Aa : Animal_T'Class := A_Cat;
begin
Go_To_Vet (Aa); -- ERROR This doesn't dynamically dispatch!
end Simple;
谢谢马克,我知道它就是这样的!我的小问题是如何将a_Cow分配给aa? (aa:= a_cow;抱怨!) – NWS 2011-05-07 14:34:28
+1很好的例子。 @NWS:分配是禁止的,正如在相邻的[answer](http://stackoverflow.com/questions/5920457/dynamic-dispatching-in-ada/5928561#5928561)中所讨论的。 – trashgod 2011-05-08 16:06:04
谢谢垃圾桶,我认为这项任务是可能的,但通过类宽指针来解决这个问题:)这正是我可能想到的反正...... – NWS 2011-05-08 18:14:22