Originale-mail to me for new edition

 

Array properties

 

Array properties are indexed properties. They can represent things like items in a list, child controls of a control, and pixels of a bitmap.

The declaration of an array property includes a parameter list that specifies the names and types of the indexes. For example,

property Objects[Index: Integer]: TObject read GetObject write SetObject;

property Pixels[X, Y: Integer]: TColor read GetPixel write SetPixel;

property Values[const Name: string]: string read GetValue write SetValue;

The format of an index parameter list is the same as that of a procedure’s or function’s parameter list, except that the parameter declarations are enclosed in brackets instead of parentheses. Unlike arrays, which can use only ordinal-type indexes, array properties allow indexes of any type.

For array properties, access specifiers must list methods rather than fields. The method in a read specifier must be a function that takes the number and type of parameters listed in the property’s index parameter list, in the same order, and whose result type is identical to the property’s type. The method in a write specifier must be a procedure that takes the number and type of parameters listed in the property index parameter list, in the same order, plus an additional value or const parameter of the same type as the property.

For example, the access methods for the array properties above might be declared as

function GetObject(Index: Integer): TObject;

function GetPixel(X, Y: Integer): TColor;

function GetValue(const Name: string): string;

procedure SetObject(Index: Integer; Value: TObject);

procedure SetPixel(X, Y: Integer; Value: TColor);

procedure SetValue(const Name, Value: string);

An array property is accessed by indexing the property identifier. For example, the statements

if Collection.Objects[0] = nil then Exit;

Canvas.Pixels[10, 20] := clRed;

Params.Values['PATH'] := 'C:\DELPHI\BIN';

correspond to

if Collection.GetObject(0) = nil then Exit;

Canvas.SetPixel(10, 20, clRed);

Params.SetValue('PATH', 'C:\DELPHI\BIN');

On Linux, you would use a path such as ‘usr/local/bin’ in place of ‘C:\DELPHI\BIN’ in the above example.

The definition of an array property can be followed by the default directive, in which case the array property becomes the default property of the class. For example,

type

  TStringArray = class

  public

    property Strings[Index: Integer]: string ...; default;

    ...

  end;

If a class has a default property, you can access that property with the abbreviation object[index], which is equivalent to object.property[index]. For example, given the declaration above, StringArray.Strings[7] can be abbreviated to StringArray[7]. A class can have only one default property. Changing or hiding the default property in descendant classes may lead to unexpected behavior, since the compiler always determines an object’s default property statically.

 

Topic groups

 

See also

Properties: Overview

Property access

 

 

译文

 

数组属性

 

数组属性(Array properties)是指可索引的属性。数组属性用来表现如列表中的项、控件中的子控件、位图中的象素等。

数组属性的声明包括一个参数列表,该列表中指定了索引的名称和类型。例如,

property Objects[Index: Integer]: TObject read GetObject write SetObject;

property Pixels[X, Y: Integer]: TColor read GetPixel write SetPixel;

property Values[const Name: string]: string read GetValue write SetValue;

索引参数列表的格式与过程或函数的参数列表的格式相同,不同的是,前者的参数声明被方括号封装,而后者的被圆括号封装。与数组不同,数组属性允许任何类型作为索引,而数组只能使用序数类型作为索引。

对于数组属性,访问说明符必需列出方法而不是域。在read说明符中的方法必需是一个函数,该函数接受属性索引参数列表中参数的个数和类型,并以相同规则(与一般属性的read方法相比)使函数的返回类型等同于属性的类型。在write说明符中的方法必需是一个过程,该过程接受属性索引参数列表中参数的个数和类型,并以相同规则(与一般属性的write方法相比)加上一个值参数或者const参数,该参数的类型与属性的类型相同。

例如,上面的数组属性的访问方法可以声明为:

function GetObject(Index: Integer): TObject;

function GetPixel(X, Y: Integer): TColor;

function GetValue(const Name: string): string;

procedure SetObject(Index: Integer; Value: TObject);

procedure SetPixel(X, Y: Integer; Value: TColor);

procedure SetValue(const Name, Value: string);

可以通过索引属性标识符来访问数组属性。例如,如下语句

if Collection.Objects[0] = nil then Exit;

Canvas.Pixels[10, 20] := clRed;

Params.Values['PATH'] := 'C:\DELPHI\BIN';

相应的内部实现是

if Collection.GetObject(0) = nil then Exit;

Canvas.SetPixel(10, 20, clRed);

Params.SetValue('PATH', 'C:\DELPHI\BIN');

Linux中,可以使用如 ‘usr/local/bin’ 的路径来代替上面语句中的 ‘C:\DELPHI\BIN’

 

数组属性的定义可以紧随一个default指示字,这时,数组属性成为类的缺省属性。例如,

type

  TStringArray = class

  public

    property Strings[Index: Integer]: string ...; default;

    ...

  end;

如果一个类含有缺省属性,那么可以通过形如 object[index] 的缩写方式访问该属性,该缩写方式等价于 object.property[index] 。例如,对于上面给出的声明序列,StringArray.String[7] 可以被缩写为 StringArray[7] 。一个类只能有一个缺省属性。在后裔类中改变或隐藏缺省属性可能导致意外的行为,因为编译器总是静态确定对象的缺省属性。

 

主题组

 

相关主题

属性:概述

属性访问