打印本文 打印本文 关闭窗口 关闭窗口
Delphi对象模型(Part IV)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2460  更新时间:2009/4/23 18:42:49  文章录入:mintao  责任编辑:mintao
//隐藏TObject.Create防止有人意外调用以试图创建TPrivateHelper的实例
    constructor Create;
      reintroduce; overload;
  end;
  TPublic = class
  private
    fHelper: TPrivateHelper;
  public
    constructor Create;
    destructor Destroy;
  end;
 
constructor TPrivateHelper.Create;
begin
  raise Exception.Create(''''Programming error'''')
end;
 
constructor TPublic.Create;
begin
  //这是唯一一个TPrivateHelper能被创建的地方
  fHelper := TPrivateHelper.Create(Self);
end;

Properties属性

属性看起来更象是字段,但可以起到与方法一样的作用。属性替代了读取者和设置者(有时也称为getter和setter),但更加机动和强大。属性对于Delphi的IDE而言非常关键,同时,我们也可以在其他许多场合使用属性。

属性由一个读者和一个写者来负责读取和设置属性的值。读者(Reader)可以是一个字段名,一个集合字段的选择器,或是返回该属性值的一个方法。写者(writer)可以是一个字段名,一个集合字段的选择器或者可以设置该属性值的一个方法。你可以省略写者,那么该属性为只读属性。当然,也可以省略掉读者以创建一个只写的属性,但是使用这么一个怪怪的属性将很受限制。同时省略读者和写者是没有意义的,因此Delphi不允许你这么做。

大多数的读者和写者是字段名称或方法名称,你也可以将其引向部分集合字段(记录和数组)。如果一个读者或写者指向一个数组元素,那么数组的索引必须是常量,并且该字段不能为动态数组。纪录和数组可以嵌套,甚至你可以使用可变类型的记录。例2-9展示了一个扩展的矩形类型,与Windows的TRect类型相似,但它是一个类,有属性和方法。

例2-9:属性的读者与写者

TRectEx = class(TPersistent)
  private
    R: TRect;
    function GetHeight: Integer;
    function GetWidth: Integer;
    procedure SetHeight(const Value: Integer);
    procedure SetWidth(const Value: Integer);
  public
    constructor Create(const R: TRect); overload;
    constructor Create(Left, Top, Right, Bottom: Integer); overload;
    constructor Create(const TopLeft, BottomRight: TPoint); overload;
 
    procedure Assign(Source: TPersistent); override;
    
    procedure Inflate(X, Y: Integer);
    procedure Intersect(const R: TRectEx);
    function IsEmpty: Boolean;
    function IsEqual(const R: TRectEx): Boolean;
    procedure Offset(X, Y: Integer);
    procedure Union(const R: TRectEx);
 
    property TopLeft: TPoint read R.TopLeft write R.TopLeft;
    property BottomRight: TPoint read R.BottomRight write R.BottomRight;
    property Rect: TRect read R write R;
    property Height: Integer read GetHeight write SetHeight;
    property Width: Integer read GetWidth write SetWidth;
  published
    property Left: Integer read R.Left write R.Left default 0;
    property Right: Integer read R.Right write R.Right default 0;
    property Top: Integer read R.Top write R.Top default 0;
    property Bottom: Integer read R.Bottom write R.Bottom default 0;
  end;

Array properties数组型属性

数组型属性总是与数量有关,并且带有数组的特性。数组型属性不能被发布,但有许多其他用途。数组的索引可以是任何类型的,并且你可以使用多维的数组。对于数组型属性而言,你必须使用读者和写者的方法,因为你没有办法将一个数组型的属性直接映射到一个数组型的字段上。

可以将其中的一个数组型属性指定为默认的。则你可以直接使用对象引用以及一个数组标号来访问该项属性而无需指明属性名称,如例子 2-10所示。

例 2-10:使用缺省的数组属性

type
  TExample = class
    ...
    property Items[I: Integer]: Integer read GetItem write SetItem;
    property Chars[C: Char]: Char read GetChar write SetChar; default;
  end;
var
  Example: TExample;
  I: Integer;
  C: Char;
begin
  Example := TExample.Create;
  I := Example.Items[4];     //必须显式的指明属性名称
  C := Example[''''X''''];         //该数组型属性时缺省的
  C := Example.Chars[''''X''''];   //效果如前
 

PartI

PartII

PartIII

PartIV

PartV

PartVI

更多文章

 

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口