我被告知Ada支持"或"布尔值之间。我的目标是能够使用支持整数类型0和1的泛型为此函数编写重载。(我知道我可以添加它们以获得返回值:如果sum> = 1,则返回1,否则返回0)I只是不知道这看起来像代码是什么。以下是我的老师希望它看起来的一个例子,这里是" *"超载:
generic
type MyType is private;
with function "*"(x,y: MyType) return MyType;
with procedure Put(x: MyType);
package gioex is
type Rectangle is
record
Length: MyType;
Width: MyType;
end record;
function Size(r: in Rectangle) return MyType;
function RectLength(r: in Rectangle) return MyType;
end gioex;
程序Put(...)和函数" *"(...)没有在泛型的主体中定义,而是在程序文件中定义:
with Ada.Text_IO; use Ada.Text_io;
with GIOEX;
procedure UseGioex2 is
package MyIntIO is new Ada.Text_IO.Integer_IO(Integer);
package MyFloatIO is new Ada.Text_IO.Float_IO(Float);
type VenusMeasure is record
F1: Integer;
F2: Float;
end record;
-- Define I/O for VenusMeasurement.
procedure Put(v: VenusMeasure) is
begin
MyIntIO.put(v.F1);
put(" ");
MyFloatIO.put(v.F2);
new_line;
end;
-- Define mutliplication for VenusMeasurement.
function "*"(p1: VenusMeasure; p2: VenusMeasure)
return VenusMeasure is
temp: VenusMeasure;
begin
temp.F1 := p1.F1 * p2.F1;
temp.F2 := p1.F2 * p2.F2;
return temp;
end;
package MyVenusRectangle
is new GIOEX(VenusMeasure, Put, "*");
use MyVenusRectangle;
width: VenusMeasure := ( 5, 5.5);
height: VenusMeasure := (3, 2.4);
Rect1: Rectangle := (width, height); -- Creates a rectangle using
-- Venus measurements.
Ans: VenusMeasure;
begin
Ans := Size( Rect1 );
end UseGIOEx2;
我是阿达的新手。我不确定为什么"用"在通用.ads文件中使用(我认为它与" *"和" Put"已经定义)有关。我的理解是,.ads文件中声明的任何过程/函数都需要在body .adb文件中定义,但是这里的" with"在实际的.adb文件中定义。澄清后,我相信我可以写我的"或"超载!谢谢!