一般问题:是否可以覆盖记录字段?覆盖记录字段
作为一个简单的例子,我想要一个Long_Integer字段,但外部世界应该只能够将其设置为Integer。
package A is
type B is record // edit: should be tagged to be able to use my intention below
c : Long_Integer;
end record;
end A;
X : B;
我可以假,我打算通过使用下面的函数/过程:
function d (Object : B) return Integer is (Integer (Object.c));
procedure d (Object : B; Value : Integer) is
begin
Object.c := Long_Integer (Value);
end d;
所以,我可以用X.d获得的价值和X.d()来设置值。
但是,是否有可能用给定的函数和过程覆盖字段“c”,并且更有趣,我可以重写它,以便我可以使用Xc:= Integer(3)来设置字段的值?
编辑:
test.adb
with Ada.Text_IO;
with A;
procedure Test is
use A;
X : C;
begin
X.d (123);
Ada.Text_IO.Put_Line("Value is " & X.d'Img);
end Test;
a.adb
package body A is
function d (Object : C) return Integer is (Integer (Object.d));
procedure d (Object : in out C; Value : Integer) is
begin
Object.d := Long_Integer (Value);
end d;
end A;
一个。广告
package A is
type B is tagged private;
type C is tagged private; -- tagged missed
type C_Access is access all C; <-- is this line correct?
function d (Object : C) return Integer;
procedure d (Object : in out C; Value : Integer);
private
type B is tagged null record;
type C is new B with record
d : Long_Integer;
end record;
end A;
编译器说: test.adb:9:04:在选定的组件无效前缀 “X” test.adb:10:39:在选定的组件无效前缀 “X”
你看,为什么?
将'C'声明为'C类型标记为private;'。 'Object.procedure(...)'或'Object.function(...)'符号只有在对象被标记时才被允许,并且从'test.adb'中的那个点只能看到'type C是私有的; ',不允许知道“C”类型被标记。 – ajb
谢谢你们两位:-) – user1091344
@ajb,@'Brian Drummond':还有一个问题:-) C_Access类型,这是正确的还是我应该将它移入私有部分? – user1091344