2

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

Ada:理解私有类型和理解封装

我正在尝试创建一个名为 的对象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):

I have created an object Configuration as shown above holding the horizontal and vertical components of Basis_R. If I want to access say the horizontal component of Configuration, the following doesn't work:

aa := Rectangular_Form.Configuration.Rectangular.Horz;

(after defining aa to be of type Long_Float)

I have tried various expressions to access the components of the object Configuration but in vain.

Any help would be most appreciated.

Thanks...

4

1 回答 1

3

The purpose of your Get_Horz and Get_Vert functions is to retrieve the components of your Rectangular object:

aa := Rectangular_Form.Get_Horz(Configuration);
于 2012-05-05T17:15:56.830 回答