我有两个套餐,分别为 Passenger 和 Booking。他们有一对多的关系。当我尝试在两个文件中执行
with
操作时,我遇到了循环依赖错误。然后我尝试了 limited with Passenger
并将 Booking 记录存储为 Passenger 作为访问类型。
package body Bookings is
procedure initialize_booking (b : in out Booking; flight : Unbounded_String; booker : Passengers.Passenger) is
begin
b.ID := nextID;
b.seat := nextSeat;
b.flight := flight;
b.booker := access booker;
nextID := nextID + 1;
end initialize_booking;
上述过程是我如何尝试将访问类型存储在预订记录中的示例。我收到“缺少操作数”错误。
有人可以解释一下我哪里出了问题吗?
如果能解释一下如何进行多对多操作,或者如何以相反的方式进行操作(即使用存储在 Passenger 中的预订访问类型的向量),我也将不胜感激。
如何使用访问类型变量来访问其中的对象并调用其方法?
感谢您的帮助。
访问类型只能作为最后的手段使用。避免此类循环依赖的方法是使用子 pkg 或第三个 pkg 来表示乘客 => 预订关系。对于儿童包装,您会有类似的东西
package Passengers is
type ID is private;
Invalid : constant ID;
private -- Passengers
type ID is record
Value : Natural := 0;
end record;
Invalid : constant ID := (Value => 0);
end Passengers;
with Passengers;
package Bookings is
type ID is private;
Invalid : constant ID;
type Info is record
Passenger : Passengers.ID;
...
end record;
function Booking_Info (Booking : in ID) return Info with
Pre => Booking /= Invalid;
-- Operations on bookings here
private -- Bookings
type ID is record
Value : Natural := 0;
end record;
Invalid : constant ID := (Value => 0);
end Bookings;
with Bookings;
package Passengers.Operations is
type Info is record
-- Might contain a set of booking IDs
end record;
function Passenger_Info (Passenger : in ID) return Info with
Pre => Passenger /= Invalid;
-- Operations on passengers here
end Passengers.Operations;
with Passengers.Operations;
package body Bookings is
...
end Bookings;
Info
类型可能也需要是私有的。将乘客操作分解为子包允许乘客独立于预订、预订with
乘客以及乘客.操作with
预订。
第 3 个 pkg 方式使 Bookings 和 Passengers 完全独立,第 3 个 pkg 提供了从 Passengers.ID 到一组 Bookings.ID 的映射。它更简单,因此可能更可取。