博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi Property详解
阅读量:4635 次
发布时间:2019-06-09

本文共 6229 字,大约阅读时间需要 20 分钟。

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

 

Description
The Property keyword defines a controlled access to class fields. 
 
It is an Object Oriented concept that internals of an object should be hidden from the outside. Whilst you can allow fields (data) in a class to be directly externally accessible (by placing in the public or published sections), this is unwise. Instead, Property can be used to define how the data is read and written. 
 
Versions 1, 2 and 3 These basic forms define read, write or read and write access to class fields. The data Type is returned from the field or method called Getter. The data is updated via the Setter field or method. 
 
Note that you must use a different name for the Name and for Getter and Setter. For example: Property Age read fAge write fAge; You would use field names when there is no vetting or retrieval processing required. When using a method to read or write, the read or written value can be a lot simpler than the stored value. The stored value can even be entirely different. 
 
Versions 4, 5 and 6 Using the Index keyword tells Delphi to pass the Constant value as the argument to the Getter and Setter methods. These must be functions that take this constant index value as an argument. 
 
For example: Property Item2 : string Index 2 read ItemGetter; where ItemGetter is defined as : Function ItemGetter(Index : Integer) : string; Default provides run time information for the property. NoDefault does not. Stored is beyond the scope of Delphi Basics. 
 
Versions 7, 8 and 9 This is a generalised version of versions 4,5 and 6. It requests the user to provide the index value for the Getter and Setter methods. 
 
Default allows the Getter and Setter method calls to be replaced as in the following example : myValue := MyClass.Getter(23); can be replaced by : myValue := MyCLass[23]; 
 
Version 10 Allows the implementation of an Interface method to be delegated to a property. Access to the property invokes the interface implementation. 
 
Version 11 By redclaring a parent class property, you can do so in a public or published clause, thereby raising the access rights of the property. 
 
Versions 12, 13 and 14 Are relevant for dispinterfaces, outside the scope of delphi basics.
 
Related commands
  Principally defines indexed class data properties
  Starts the section of private data and methods in a class
  Starts a section of class private data accesible to sub-classes
  Starts an externally accessible section of a class
  Starts a published externally accessible section of a class
 
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;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
  // Class with Indexed properties
TRectangle = class
private
fArea : 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 itself
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// TRectangle property 'Getter' routine
// TRectangle constructor
constructor TRectangle.Create;
begin
  // Give default rectangle coordinates
left := 0;
right := 100;
top := 0;
bottom := 100;
fArea := 100 * 100;
end;
function TRectangle.GetCoord(Index: Integer): Longint;
begin
  // Only allow valid index values
if (Index >= 0) and (Index <= 3)
then Result := fCoords[Index]
else Result := -1;
end;
// TRectangle property 'Setter' routine
procedure TRectangle.SetCoord(Index, Value: Integer);
begin
  // Only allow valid index values
if (Index >= 0) and (Index <= 3)
then
begin
    // Save the new value
fCoords[Index] := Value;
    // And recreate the rectangle area
fArea := (right - left) * (bottom - top);
end;
end;
// Main line code
procedure TForm1.FormCreate(Sender: TObject);
var
myRect : TRectangle;
i : Integer;
begin
  // Create my little rectangle with default coordinates
myRect := TRectangle.Create;
  // And set the corner coordinates
myRect.Left := 22; // Left using direct method
myRect.Top := 33;
myRect.SetCoord(2,44); // Right using indexed method
myRect.SetCoord(3,55);
  // And ask for these values
for i:= 0 to 3 do
ShowMessageFmt('myRect coord %d = %d',[i,myRect.GetCoord(i)]);
  // And show the rectangle area
ShowMessageFmt('myRect area = %d',[myRect.Area]);
end;
end.
myRect coord 0 = 22
myRect coord 1 = 33
myRect coord 2 = 44
myRect coord 3 = 55
myRect area = 484

 

转载于:https://www.cnblogs.com/FKdelphi/p/10836623.html

你可能感兴趣的文章
一篇讲Java指令重排和内存可见性的好文
查看>>
二叉查找树,红黑树
查看>>
jquery评分效果Rating精华版
查看>>
HDOJ1004
查看>>
Eclipse中部分快捷键
查看>>
LintCode: Unique Characters
查看>>
从一个OutOfMemoryError 学会了分析Java内存泄漏问题
查看>>
Delphi TScrollBar 用于滚动窗口、组件内容
查看>>
【微信开发】上传下载多媒体文件
查看>>
java道路级别
查看>>
扩展方法
查看>>
vue事件
查看>>
Docker在Ubuntu16.04上安装
查看>>
python爬虫学习之页面登陆
查看>>
SPOJ-OPTM Optimal Marks ★★(按位建图 && 最小割)
查看>>
H264/AVC视频解码时AVC1和H264的区别
查看>>
SRAM与SDRAM的区别
查看>>
如何不使用pthread_cancel而杀死线程
查看>>
[笔记]VI编辑器的学习
查看>>
过滤器与拦截器区别
查看>>