Ada:使用构造对象的值

时间:2012-05-05 15:50:32

标签: oop package ada

这篇文章是上一篇文章的后续内容:

Ada: Understanding private types and understanding packaging

我正在尝试创建一个名为Configuration的对象,在屏幕上打印它以便我可以看到它的内容并尝试访问这个创建对象的组件。 这是给我带来麻烦的最后一部分。代码如下:

首先是包规范Rectangular_Method.ads

package Rectangular_Method is
type Rectangular is private;

function Construct(Horz, Vert : Long_Float) return Rectangular;

procedure Print(Configuration: in Rectangular);

procedure Vector_Basis_r (A : in Long_Float; D : out Rectangular);

function Get_Horz (R : Rectangular) return Long_Float;
function Get_Vert (R : Rectangular) return Long_Float;

private
type Rectangular is
    record
         Horz, Vert: Long_Float;
    end record;

end Rectangular_Method;

接下来,包体Rectangular_Method.adb

with Ada.Text_IO, Ada.Long_Float_Text_IO;
with Ada.Numerics.Long_Elementary_Functions;
use  Ada.Numerics.Long_Elementary_Functions;

package body Rectangular_Method is

function Construct(Horz, Vert : Long_Float) return Rectangular is
begin
  return(Horz, Vert);
end Construct;

procedure Print(Configuration: in Rectangular) is
use Ada.Text_IO, Ada.Long_Float_Text_IO;
  begin
Put("(");
Put(Configuration.Horz, Fore => 2, Aft => 2, Exp => 0); Put(", ");
Put(Configuration.Vert, Fore => 2, Aft => 2, Exp => 0);
Put_Line(")");
  end Print;

procedure Vector_Basis_r (A : in Long_Float; D : out Rectangular) is
begin
  D.Horz := Cos (A, Cycle => 360.0);
  D.Vert := Sin (A, Cycle => 360.0);
end Vector_Basis_R;

function Get_Horz (R : Rectangular) return Long_Float is
begin
  return R.Horz;
end Get_Horz;

function Get_Vert (R : Rectangular) return Long_Float is
begin
  return R.Vert;
end Get_Vert;
end Rectangular_Method;

最后是测试文件test_rectangular_form.adb

with Ada.Long_Float_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;

with Rectangular_Form;
use type Rectangular_Form.Rectangular;
procedure Test_Rectangular_Form is

Theta                                 : Long_Float;
Basis_r                               : Rectangular_Form.Rectangular;

Configuration: Rectangular_Form.Rectangular;

begin
   Ada.Text_IO.Put("Enter the angle ");
   Ada.Long_Float_Text_IO.Get (Item => theta);

   --Vector basis
   Rectangular_Form.Vector_Basis_R (A => Theta, D => Basis_R);

   Configuration := Rectangular_Form.Construct(Rectangular_Form.Get_Horz (Basis_R),Rectangular_Form.Get_Vert (Basis_R));
   Ada.Text_IO.New_Line;
   Rectangular_Form.Print(Configuration);

end Test_Rectangular_Form;

现在问题(基于test_rectangular_form.adb):

如上所示,我创建了一个对象Configuration,其中包含 Basis_R 水平垂直组件。如果我想访问Configuration的水平分量,则以下内容不起作用:

aa := Rectangular_Form.Configuration.Rectangular.Horz;

(将aa定义为 Long_Float 类型后)

我尝试过各种表达式来访问对象Configuration的组件但是徒劳无功。

非常感谢任何帮助。

...谢谢

1 个答案:

答案 0 :(得分:3)

Get_Horz和Get_Vert函数的目的是检索Rectangular对象的组件:

aa := Rectangular_Form.Get_Horz(Configuration);