Ada 继承和多态小例
三个文件,一条命令构建。
-- 文件:oo.ads
package OO is
type Object is tagged record
X : Float;
Y : Float;
end record;
function Area (Obj : Object) return Float;
type Point is new Object with null record;
type Circle is new Object with record
Raduis : Float;
end record;
overriding function Area (Cir : Circle) return Float;
procedure Put_Area (Obj : Object'Class);
end OO;
-- 文件:oo.adb
with Ada.Text_IO;
use Ada.Text_IO;
package body OO is
function Area(Obj : Object) return Float is
begin
return 0.0;
end Area;
function Area (Cir : Circle) return Float is
begin
return 3.24 * Cir.Raduis ** 2;
end Area;
procedure Put_Area (Obj : Object'Class) is
begin
Put_Line (Area(Obj)'Image);
end Put_Area;
end OO;
--文件:main.adb
with Ada.Text_IO;
use Ada.Text_IO;
with OO;
procedure Main is
use OO;
Obj : Object := (0.0, 0.0);
Poi : Point := (0.0, 0.0);
C : Circle := (0.0, 0.0, 1.0);
begin
Put(Obj.X'Image);
Put(Obj.Y'Image);
New_Line;
Put_Area(Obj);
Put_Area(Poi);
Put_Area(C);
end Main;
#构建
gnatmake main.adb