是的,当然可以。嗯,有点。
但就像大多数 Ada 式的东西一样,它需要一点思考和计划。
这是一种方式(唯一的方式?)
相应的声明是,
package GrandParent is
type Item is private;
private
type Item is
record
Value : Boolean;
end record;
end GrandParent;
package GrandParent.Parent is
function Get
(The_Item : in Item)
return Boolean;
end GrandParent.Parent;
private package GrandParent.Child1 is
procedure Set
(The_Item : in out Item;
Value : in Boolean);
end GrandParent.Child1;
包体是,
package body GrandParent.Child1 is
procedure Set
(The_Item : in out Item;
Value : in Boolean)
is
begin
The_Item.Value := Value;
end Set;
end GrandParent.Child1;
private with GrandParent.Child;
package body GrandParent.Parent is
function Get
(The_Item : in Item)
return Boolean
is
(The_Item.Value);
procedure Set
(The_Item : in out Item;
Value : in Boolean)
is
begin
GrandParent.Child.Set
(The_Item => The_Item,
Value => Value);
end Set;
end GrandParent.Parent;
如果你尝试拥有,
(private) with GrandParent.Child;
package GrandParent.Parent.Child is
end GrandParent.Parent.Child;
这会引发编译时错误,即当前单元也必须是 GrandParent 的直接后代,从而有效地使 GrandParent.Child1 包对 GrandParent.Parent 私有。
GrandParent 的客户也无法看到 GrandParent.Child1。但是,GrandParent 的其他孩子将具有与 GrandParent.Parent 相同的可见性
这就是隐藏 Set 子程序的方法。如果你想对子包隐藏私有类型怎么办?
首先,这可能是有问题的,因为具有私有类型的包的子级旨在与该类型完全交互,因为正如其他人所描述的那样,子级正在扩展其各自父级包的功能。
如果你想这样做,那么你最好的办法是将类型 Item 以及 Get 和 Set 例程都隐藏到 GrandParent.Child 中,这样只有 GrandParent.Parent 才能看到它们(在它的私有正文中)并公开任何内容您希望 GrandParent.Parent 的孩子在 GrandParent.Parent 包中拥有的功能。
但是,我不确定这是否特别有用。一个问题 - 如果 Parent 的孩子不应该访问 Item 的内部工作,为什么他们是 Parent 的孩子?