TStrings类型的对象可以用数组运算符 [] 来取其中的元素的原因

TStrings类型的对象可以用数组运算符  []  来取其中的元素的原因;
我们来看看TStrings是怎么定义的,下面给出源代码,其中省略了一些无关紧要的定义,

 TStrings = class(TPersistent)
  private
    FDefined: TStringsDefined;
    FDelimiter: Char;
     ......
  protected
    procedure DefineProperties(Filer: TFiler); override;
    procedure Error(const Msg: string; Data: Integer); overload;
    .........
  public
    destructor Destroy; override;
    function Add(const S: string): Integer; virtual;
    ........
    property Strings[Index: Integer]: string read Get write Put; default;
    property Text: string read GetTextStr write SetTextStr;
    property StringsAdapter: IStringsAdapter read FAdapter write SetStringsAdapter;
  end;

我们注意到其中有这么一行代码:
property Strings[Index: Integer]: string read Get write Put; default; 
在这个代码的后面有default 关键字.呵呵,就是它了,由于它的存在,下面的两种操作是等效的.

list:TStings;

list[i]; 等效于 list.Strings[i]

最近看了c#之后发现他也有这样的特性,所以我估计这是一种被delphi证明了的广大程序员较为喜欢的特性。
posted @ 2006-09-24 16:36  拿走不谢  阅读(425)  评论(0)    收藏  举报