http://anony3721.blog.163.com/blog/static/51197420107105132120/?ignoreua

 Property
Keyword Defines controlled access to class fields System unit
?.Property Name : Type read Getter|nodefault;
?.Property Name : Type write Setter;
?.Property Name : Type read Getter write Setter;
?.Property Name : Type Index Constant read Getter {default : Constant|nodefault;} {stored : Boolean};
?.Property Name : Type Index Constant write Setter {default : Constant|nodefault;} {stored : Boolean};
?.Property Name : Type Index Constant read Getter write Setter {default : Constant|nodefault;} {stored : Boolean};
?.Property Name[Index : IndexType] : BaseType read Getter {default;}
?.Property Name[Index : IndexType] : BaseType write Setter; {default;}
?.Property Name[Index : IndexType] : BaseType read Getter write Setter; {default;}
.Property Name : Type read Getter implements Interfaces...;
.Property Name; // Redeclared base class property
.Property Name : Type; // Dispinterface only
.Property Name : Type readonly; // Dispinterface only
.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
Index   Principally defines indexed class data properties
Private   Starts the section of private data and methods in a class
Protected   Starts a section of class private data accesible to sub-classes
Public   Starts an externally accessible section of a class
Published   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.

Show full unit code
myRect coord 0 = 22
myRect coord 1 = 33
myRect coord 2 = 44
myRect coord 3 = 55
myRect area = 484

Delphi Property详解的更多相关文章

  1. Delphi 关键字详解

    Delphi 关键字详解[整理于 "橙子" 的帖子] absolute //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同. var   Str: ];   S ...

  2. Objective-C中的@Property详解

    Objective-C中的@Property详解 @Property (属性) class vairs 这个属性有nonatomic, strong, weak, retain, copy等等 我把它 ...

  3. Delphi指针详解

    Delphi指针详解2007-12-04 06:08:57|  分类: DLL学习 阅读91 评论0   字号:大中小 订阅 大家都认为,C语言之所以强大,以及其自由性,很大部分体现在其灵活的指针运用 ...

  4. Delphi多线程详解

    (整理自网络) Delphi多线程处理 1-1多线程的基本概念 WIN 98/NT/2000/XP 是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU 运行时间和资源,或者 ...

  5. Delphi 关键字详解[整理于 "橙子" 的帖子]

    absolute //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同. var   Str: ];   StrLen: Byte absolute Str; //这个声明指定了变量 ...

  6. 【转】Delphi 关键字详解

    absolute //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同. var Str: string[32]; StrLen: Byte absolute Str; //这个声明 ...

  7. Delphi函数详解:全局函数,内部函数,类的成员函数,类的静态方法

    1. Delphi中的全局函数 //要点: 需要给其他单元调用, 必须在 interface 声明, 但必须在 uses 区后面 unit Unit1; interface uses   Window ...

  8. OC中的@property详解

    简介: @property 生成了变量的get set 方法,同时指定了变量名称. 例如@property (nonatomic,strong) NSString *name;表示生成了_name私有 ...

  9. python property详解

    Python中有一个被称为属性函数(property)的小概念,它可以做一些有用的事情.在这篇文章中,我们将看到如何能做以下几点: 将类方法转换为只读属性 重新实现一个属性的setter和getter ...

随机推荐

  1. mipmap一

    讲一下mipmap 生成的时候 指定texture 的mipmaplevel 8 然后memory自动就会那么大的 画的时候要在lineraspace(因为我是gpucopy所以不涉及这个问题 可能) ...

  2. 摄像头bug查找工作总结

    近期花了很长时间在libcamera中查找和解决一个bug.下面将这段时间中的工作过程,以及对camera的认识总结如下: 首先是问题的发生,在UM2801中,摄像头的代码已经基本实现,并且相应功能也 ...

  3. 深入理解ES6里的promise

    一.ES6 Promise是什么? 复杂的概念先不讲,我们先简单粗暴地把Promise用一下,有个直观感受.那么第一个问题来了,Promise是什么呢?是一个类?对象?数组?函数? 别猜了,直接打印出 ...

  4. How to support both ipv4 and ipv6 address for JAVA code.

    IPv6 have colon character, for example FF:00::EEIf concatenate URL String, IPv6 URL will like: http: ...

  5. .Net程序测试阿里云OSS开放存储服务

    阿里云官网有提供OSS相关的操作API文档和.Net程序的 SDK,也可以在这里下载OSS相关文件 但是API文档里面的都是通过http请求和响应的消息来描述如何操作OSS的 而一般在程序中需要的是O ...

  6. ASP.NET MVC学习---(九)权限过滤机制(完结篇)

    相信对权限过滤大家伙都不陌生 用户要访问一个页面时 先对其权限进行判断并进行相应的处理动作 在webform中 最直接也是最原始的办法就是 在page_load事件中所有代码之前 先执行一个权限判断的 ...

  7. 转:全面分析 Spring 的编程式事务管理及声明式事务管理

    转:from: https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/

  8. AndroidStudio怎么实现微信分享功能

    在应用中添加微信分享功能,需要在微信开放平台上传你的应用,审核通过后方可使用此功能. https://open.weixin.qq.com/网址 申请的过程比较简单,这里就不追溯了,贴一个友情链接 h ...

  9. C#程序不包含适合于入口点的静态“Main”方法怎么办

    如下图所示,一般程序上次运行还好好的,而且不管你复制粘贴再简单的程序也出现这种错误提示.   先点击右侧的显示所有文件,下面列举了所有CS文件,右击点击包括在项目中,则该文件呈现绿色,再运行即可.不过 ...

  10. C++ 利用文件流复制文件

    bool CopyFile(const std::string &src, const std::string &dest) { std::ifstream fin(src.c_str ...