胡里奧,在規範文件中聲明的類型(mytypes.ads)
package Mytypes is
type Fruit is (Apple, Pear, Pineapple, Banana, Poison_Apple);
subtype Safe_Fruit is Fruit range Apple .. Banana;
end Mytypes;
... 在其他幾個Withed它:
with Mytypes;
package Parent is
function Permission (F : in Mytypes.Fruit) return Boolean;
end Parent;
...
package body Parent is
function Permission (F : in Mytypes.Fruit) return Boolean is
begin
return F in Mytypes.Safe_Fruit;
end Permission;
end Parent;
...
package Parent.Child is
procedure Eat (F : in Mytypes.Fruit);
end Parent.Child;
...
with Ada.Text_Io;
package body Parent.Child is
procedure Eat (F : in Mytypes.Fruit) is
begin
if Parent.Permission (F) then
Ada.Text_Io.Put_Line ("Eating " & Mytypes.Fruit'Image (F));
else
Ada.Text_Io.Put_Line ("Forbidden to eat " & Mytypes.Fruit'Image (F));
end if;
end Eat;
end Parent.Child;
...
with Mytypes;
with Parent.Child;
procedure Main is
begin
for I in Mytypes.Fruit'Range loop
Parent.Child.Eat (I);
end loop;
end Main;
它編譯:
$ gnatmake main.adb
gcc-4.4 -c parent-child.adb
gnatbind -x main.ali
gnatlink main.ali
運行:
$ ./main
Eating APPLE
Eating PEAR
Eating PINEAPPLE
Eating BANANA
Forbidden to eat POISON_APPLE
這是你嘗試過什麼?
來源
2012-05-10 14:34:15
NWS
我有一個回答(http://stackoverflow.com/a/10515906/40851)在回答你的其他問題..使用兄弟軟件包而不是子包。 –