http://anony3721.blog.163.com/blog/static/51197420107105132120/?ignoreua
1 Property 2 Keyword Defines controlled access to class fields System unit 3 ?1.Property Name : Type read Getter|nodefault; 4 ?2.Property Name : Type write Setter; 5 ?3.Property Name : Type read Getter write Setter; 6 ?4.Property Name : Type Index Constant read Getter { default : Constant|nodefault;} { stored : Boolean}; 7 ?5.Property Name : Type Index Constant write Setter { default : Constant|nodefault;} { stored : Boolean}; 8 ?6.Property Name : Type Index Constant read Getter write Setter { default : Constant|nodefault;} { stored : Boolean}; 9 ?7.Property Name[Index : IndexType] : BaseType read Getter { default;}10 ?8.Property Name[Index : IndexType] : BaseType write Setter; { default;}11 ?9.Property Name[Index : IndexType] : BaseType read Getter write Setter; { default;}12 10.Property Name : Type read Getter implements Interfaces...;13 11.Property Name; // Redeclared base class property14 12.Property Name : Type; // Dispinterface only 15 13.Property Name : Type readonly; // Dispinterface only 16 14.Property Name : Type writeonly; // Dispinterface only
|
Example code : Illustrating basic and indexed properties |
// Full Unit code.// -----------------------------------------------------------// You must store this code in a unit called Unit1 with a form// called Form1 that has an OnCreate event called FormCreate.unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;type // Class with Indexed propertiesTRectangle = classprivatefArea : LongInt;fCoords : array[0..3] of Longint;function GetCoord(Index: Integer): Longint;procedure SetCoord(Index: Integer; Value: Longint);public Property Area : Longint read fArea; Property Left : Longint Index 0 read GetCoord write SetCoord; Property Top : Longint Index 1 read GetCoord write SetCoord; Property Right : Longint Index 2 read GetCoord write SetCoord; Property Bottom : Longint Index 3 read GetCoord write SetCoord; Property Coords[Index: Integer] : Longint read GetCoord write SetCoord;constructor Create;end; // The form class itselfTForm1 = class(TForm)procedure FormCreate(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}// TRectangle property 'Getter' routine// TRectangle constructorconstructor TRectangle.Create;begin // Give default rectangle coordinatesleft := 0;right := 100;top := 0;bottom := 100;fArea := 100 * 100;end;function TRectangle.GetCoord(Index: Integer): Longint;begin // Only allow valid index valuesif (Index >= 0) and (Index <= 3)then Result := fCoords[Index]else Result := -1;end;// TRectangle property 'Setter' routineprocedure TRectangle.SetCoord(Index, Value: Integer);begin // Only allow valid index valuesif (Index >= 0) and (Index <= 3)thenbegin // Save the new valuefCoords[Index] := Value; // And recreate the rectangle areafArea := (right - left) * (bottom - top);end;end;// Main line codeprocedure TForm1.FormCreate(Sender: TObject);varmyRect : TRectangle;i : Integer;begin // Create my little rectangle with default coordinatesmyRect := TRectangle.Create; // And set the corner coordinatesmyRect.Left := 22; // Left using direct methodmyRect.Top := 33;myRect.SetCoord(2,44); // Right using indexed methodmyRect.SetCoord(3,55); // And ask for these valuesfor i:= 0 to 3 doShowMessageFmt('myRect coord %d = %d',[i,myRect.GetCoord(i)]); // And show the rectangle areaShowMessageFmt('myRect area = %d',[myRect.Area]);end;end. |
myRect coord 0 = 22myRect coord 1 = 33myRect coord 2 = 44myRect coord 3 = 55myRect area = 484 |