The optional stored, default,
and nodefault directives are called storage specifiers.
They have no effect on program behavior, but control the way runtime type
information (RTTI) is maintained. Specifically, storage specifiers determine
whether or not to save the values of published properties in form files.
The stored directive must
be followed by True, False, the name of a Boolean field,
or the name of a parameterless method that returns a Boolean value. For
example,
property Name:
TComponentName read FName write SetName stored False;
If a property has no stored directive, it is treated as if stored True were specified.
The default directive must
be followed by a constant of the same type as the property. For example,
property Tag:
Longint read FTag write FTag default 0;
To override an inherited default
value without specifying a new one, use the nodefault directive. The default
and nodefault directives are supported only for ordinal types and for
set types, provided the upper and lower bounds of the set’s base type have
ordinal values between 0 and 31; if such a property is declared without default
or nodefault, it is treated as if nodefault were specified. For
reals, pointers, and strings, there is an implicit default value of 0, nil,
and '' (the empty string), respectively.
When saving a component’s state, the storage specifiers of the component’s published properties are checked. If a property’s current value is different from its default value (or if there is no default value) and the stored specifier is True, then the property’s value is saved. Otherwise, the property’s value is not saved.
Note: Storage specifiers are not supported for array properties. The default directive has a different meaning when used in an array property declaration. See Array properties.
可选的指示字stored、default和nodefault被统称为存储说明符(storage specifiers)。存储说明符不影响程序的行为特性,但可以控制运行时信息(RTTI)的维护途径。明确地说,存储说明符决定了是否保存或不保存窗体文件(form files)中公布属性的值
指示字stored之后必需跟随True、False、Boolean域的名称,或者返回Boolean值的无参数方法的名称。例如,
property Name:
TComponentName read FName write SetName stored False;
如果属性声明中没有stored指示字,那么将被视为
stored True 。
指示字default之后必需跟随一个与当前属性类型相同的常量。例如,
property Tag:
Longint read FTag write FTag default 0;
要覆盖一个继承的default值而不指定新的值,应使用指示字nodefault。指示字default和nodefault只支持序数类型和集合类型(集合基类型上下界范围的序数值在0到31之间);如果这样的属性在声明时没有指定default或nodefault说明符,将被视为指定了nodefault说明符。对于实数、指针、串等,分别隐含地有各自的缺省值0、nil、’’(空串)。
保存组件状态的时候,组件中公布属性的存储说明符被检查。如果一个属性的当前值与指示字default指定的缺省值(或者没有缺省值)不同并且stored指示为True时,那么属性的值被保存。否则,属性的值不被保存。
注意:存储说明符不支持数组属性。指示字default用于数组属性时具有不同的(与作为存储说明符时相比)特殊含义,见数组属性。
编者注
指示字stored
严格来说,作为存储说明符,指示字stored之后可以跟随下列任意之一:
·True或者与之相等的布尔常量;
·False或者与之相等的布尔常量;
·Boolean类型(或与之兼容的任何布尔类型)域的名称;
·返回Boolean类型(或与之兼容任何布尔类型)值的无参数方法的名称。
这里的描述的范围比原文中描述的范围要大。例如,可以在指示字stored之后跟随WordBool类型的域的名称,这在通过原文帮助中是体现不出来的。此外,还可以是用户自定义的任何布尔类型。
存储说明符的使用范围
存储说明符的用意在于是否保存公布属性时提供判定依据,这对开发者设计和编程是至关重要的。在设计时,开发者能够通过对象检查器(Object Inspector)等途径查看和修改所有公布(published)属性的值,而无法获取公共(public)属性、保护(protected)属性和私有(private)属性的值。因此,存储说明符有意义的位置是用于公布属性。事实上,Borland编译器对置于公共属性、保护属性甚至私有属性的存储说明符都允许。或许这是为了在后裔类中提高属性可见度时提供方便吧。但编者仍然推荐只在公布属性中使用存储说明符。