| Delphi对象模型 (PART V) |
|
| Delphi对于面向对象编程的支持丰富而且强大。除了传统的类和对象,Delphi还提供了接口,异常处理,多线程编程等特性。这一章节深入讲解了Delphi的对象模型。读者应当对标准的Pascal比较熟悉,并且对有关面向对象编程的基本法则有一定了解。 (本文的英文原文将Delphi与Object Pascal统一表述为Delphi,可能有概念不清之嫌疑。但在大多数情况下,相信读者能够根据上下文来判定文中所述之Delphi的具体含义——译者注。) |
Indexed properties索引型的属性你可以将许多不同的属性映射到一个相同的读或写的方法,只需为每个属性指定一个索引值。这个索引值将被传递给读或写的方法以区别不同属性。 你甚至可以将数组型和索引型混合使用。读者和写者的方法将会区分他们——将数组的索引作为第一个参数,接着才是索引值。 Default values默认值属性还用到stored和default两个指示符。这里的信息与 Delphi的Object Pascal语言的特性关系不大,但是Delphi的IDE将其用作保存Form的描述。stored指示符的值可以是一个Boolean类型的常量,也可以是一个Boolean型的字段,或是一个不需任何参数直接返回Boolean值方法。default指示符的值应当是与该属性的类型相同的一个常量。只有枚举型,整形以及集合类型的属性可以有一个缺省(default)的值。stored和default指示符只对发布的属性才有意义。 为了将缺省的数组数型与缺省的值区别开来,缺省数组的指示符以一个分号跟在属性的声明后面。缺省值指示符则直接置于属性声明以后。参见第五章了解有关default指示符的内容。 Using properties使用属性通常情况下定义一个类时,我们将所有的字段设置为私有的,然后声明许多公开的属性来访问他们。然而Delphi并未对属性直接访问字段方式表示不满。但是使用属性,你可以在未来的某个时刻改变属性的实现,比如在字段值发生改变是增加校验等。你也可以使用属性来强制访问级别,比如当一个字段的之不能被改变时使用一个只读的属性。例 2-11显示的是声明和使用属性的几种方式。 例 2-11:声明和使用属性 type TCustomer = record Name: string; TaxIDNumber: string[9]; end; TAccount = class private fCustomer: TCustomer; fBalance: Currency; fNumber: Cardinal; procedure SetBalance(NewBalance: Currency); published property Balance: Currency read fBalance write SetBalance; property Number: Cardinal read fNumber; //不可改变 property CustName: string read fCustomer.Name; end; TSavingsAccount = class(TAccount) private fInterestRate: Integer; published property InterestRate: Integer read fInterestRate write fInterestRate default DefaultInterestRate; end; TLinkedAccount = class(TObject) private fAccounts: array[0..1] of TAccount; function GetAccount(Index: Integer): TAccount; public //两种属性访问数组的方法:使用索引或者引用一个数组元素 property Checking: TAccount index 0 read GetAccount; property Savings: TAccount read fAccounts[1]; end; TAccountList = class private fList: TList; function GetAccount(Index: Integer): TAccount; procedure SetAccount(Index: Integer; Account: TAccount); function GetCount: Integer; protected property List: TList read fList; public property Count: Integer read GetCount; property Accounts[Index: Integer]: TAccount read GetAccount write SetAccount; default; end; procedure TAccount.SetBalance(NewBalance: Currency); begin if NewBalance < 0 then raise EOverdrawnException.Create; fBalance := NewBalance; end; function TLinkedAccount.GetAccount(Index: Integer): TAccount; begin Result := fAccounts[Index] end; function TAccountList.GetCount: Integer; begin Result := List.Count end; function TAccountList.GetAccount(Index: Integer): TAccount; begin Result := List[Index] end; procedure TAccountList.SetAccount(Index: Integer; Account: TAccount); begin fList[Index] := Account end; |
Class-type properties对象类型的属性对象类型的属性需要引起格外的关注。使用对象类型的时候,最好由对象的拥有者负责管理对象属性。也就是说,单单保存一个对象引用是不够的,需要保留一分该对象属性的一个副本。是用一个写者方法来做到这一点。Delphi的IDE要求所有发布的属性满足这个要求,同时也对未发布的属性也产生影响。 此规则的唯一的例外是,属性保存的是对Form上的组件的引用。这种情况下,属性必须保存对象引用而非组件的副本。 Delphi的IDE只在.dfm中存放组件名称以保存组件引用的值。当.dfm被装载时,Delphi查找组件名以恢复对象引用的值。如果你必须要在一个组件内部保存一个完整的组件,则你必须实现对内部组件的属性的访问委托。 确认属性的类继承自TPersistent而来,并且该类覆盖了Assign方法。通过调用Assign来实现属性的写方法。(TPersistent,在Classes单元中定义,并不是必须的,但确是一个最简单的方法——复制一个对象。否则,你将花费两倍的代价在任何其他用到的类中书写Assigned方法。)读方法可以提供对字段的直接访问。如果该对象有一个OnChange的事件,你最好将设置其值以了解对象何时作了改变。例子 2-12显示了一个典型的使用对象属性的方法。例子中定义了一个图形控件,用于在需要时在其范围内以平铺的方式显示位图。属性Bitmap存放了一个TBitmap对象。 例 2-12:声明和使用对象类型的属性 unit Tile; interface uses SysUtils, Classes, Controls, Graphics; type // Tile a bitmap TTile = class(TGraphicControl) private fBitmap: TBitmap; procedure SetBitmap(NewBitmap: TBitmap); procedure BitmapChanged(Sender: TObject); protected procedure Paint; override; public constructor Create(Owner: TComponent); override; destructor Destroy; override; published property Align; property Bitmap: TBitmap read fBitmap write SetBitmap; property OnClick; property OnDblClick; //还有许多有用的方法,限于空间不一一列出。详见TControl。 end; implementation { TTile } // Create the bitmap when creating the control. constructor TTile.Create(Owner: TComponent); begin inherited; fBitmap := TBitmap.Create; fBitmap.OnChange := BitmapChanged; end; // Free the bitmap when destroying the control. destructor TTile.Destroy; begin FreeAndNil(fBitmap); inherited; end; // When the bitmap changes, redraw the control. procedure TTile.BitmapChanged(Sender: TObject); begin Invalidate; end; // Paint the control by tiling the bitmap. If there is no // bitmap, don't paint anything. procedure TTile.Paint; var X, Y: Integer; begin if (Bitmap.Width = 0) or (Bitmap.Height = 0) then Exit; Y := 0; while Y < ClientHeight do begin X := 0; while X < ClientWidth do begin Canvas.Draw(X, Y, Bitmap); Inc(X, Bitmap.Width); end; Inc(Y, Bitmap.Height); end; end; //通过复制TBitmap对象的方式设置新值 procedure TTile.SetBitmap(NewBitmap: TBitmap); begin fBitmap.Assign(NewBitmap); end; end.PartI PartII PartIII PartIV PartV PartVI 更多文章
|
