property用法

22222bbb 2008-07-28 04:48:09
type
TclsUser=Class(TObject)
private
PID:String;
PName:String;
PDesc:String;
PType:integer;
PExternal:integer;
PPassword:String;
PAID:String;
Pbuilder:string;
PbDate:string;
Previser:string;
PrDate:string;
public
property FID:String read PID write PID;
property FName:String read PName write PName;
property FDesc:String read PDesc write PDesc;
property FType:integer read PType write PType;
property FExternal:integer read PExternal write PExternal;
property FPassword:String read PPassword write PPassword;
property FAID:String read PAID write PAID;
property Fbuilder:string read Pbuilder write Pbuilder;
property FbDate:string read PbDate write PbDate;
property Freviser:string read Previser write Previser;
property FrDate:string read PrDate write PrDate;
end;
正如上面代码:property 后面的read和write是什么意思啊.如果能结合整段代码给个解释说明这个有什么有就太感谢了.详细点好.欢迎"僵哥"光临
...全文
311 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyd531 2008-07-29
  • 打赏
  • 举报
回复
我们在delphi的类中常常能看到这样的代码:property 属性名 类型名 read 字符串1 write 字符串2

这里属性的名字可能不同。都是这样的格式:property 属性名 read 字符串1 write 字符串2

我以property Left: Integer read FLeft write SetLeft;为例子,它是Tcontrol的属性,你可以在controls文件中找到。Left是一个Integer类型的属性。Read申明了访问该变量要访问的变量或者方法,write申明了修改该变量时访问的变量或者方法。注意:可以是变量,也可以是方法,我在后面告诉大家这是怎么回事。这里它是一个变量,名字叫做FLeft。出于封装的目的,我们一般都会把这样的变量放在private中间去,果然,在private中我们可以找到

FLeft: Integer这段代码(出于命名的习惯,我们把这样的变量取名为属性名前面加一个大写的F)。这样当你read该属性时,实际上你访问的是Fleft的值。所以你可以写些方法来修改fleft,间接修改了left的值。然后我们再看SetLeft,这里它是一个方法(问我怎么知道?还是看命名规则,通常用属性名前面加上Set),通常也会放在private中去,我们来验证一下,我们在private中看到申明:

procedure SetLeft(Value: Integer);

和如下代码实现:

procedure TControl.SetLeft(Value: Integer);

begin

SetBounds(Value, FTop, FWidth, FHeight);

Include(FScalingFlags, sfLeft);

end;

如果你写了如下代码改变left:control1.left:=23,那么程序调用了函数SetLeft(23),SetBounds是改变区域的函数,这里你就明白了它封装了的好处,每次你改变left时它就会根据新的left而改变区域的大小,这个函数同时也改变了Fleft的大小,请查阅SetBounds的源代码。

procedure TControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);

begin

if CheckNewSize(AWidth, AHeight) and




((ALeft <> FLeft) or (ATop <> FTop) or

(AWidth <> FWidth) or (AHeight <> FHeight)) then

begin

InvalidateControl(Visible, False);

FLeft := ALeft;
FTop := ATop;

FWidth := AWidth;

FHeight := AHeight;

UpdateAnchorRules;

Invalidate;

Perform(WM_WINDOWPOSCHANGED, 0, 0);

RequestAlign;

if not (csLoading in ComponentState) then Resize;

end;

end;

这样外部就看起来只是通过赋值运算来改变了该属性的值。Read和write可以是变量,或者是函数,取决于你的设计。你当然可以这样写: propert property 属性名 类型名 read 变量1 write 变量2。变量1和变量2可以是相同的。你也可以这样propert property 属性名 类型名 read 方法1 write 方法2。任你组合。但是有2点要注意:

1. 命名规则最好按习惯来,易于阅读。

2. 如果是变量,那么类型要和属性的类型一致,如果是方法,那么入口参数要和属性的类型一致。




二事件属性Tevent

我们常常使用组件的事件属性,比方说click事件,可是我们很难从表面看出它是如何调用的呢,如何触发的呢。下面我来给你解答。

我们在属性管理器object inspector中看到event页onclick右边对应了一个方法的名字。我们其实可以这样给一个组件的事件对应上一个出来方法。以一个form为例子Form1. OnMouseDown:=‘你的方法‘。注意方法的入口参数有讲究,这里是(Sender:TObject)

我们还是一tcontrol为例子,我们找到这段代码:

property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;跟上面讲的类似,不过这里有个特殊的类型,TNOtifyEvent,是个事件类型,我们找到它的申明:

TMouseEvent = procedure(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer) of object;

可以看到,它其实就是个函数,但是蓝色部分把入口参数限定了。那么我们通过赋值Form1. OnMouseDown:=‘你的方法‘,就对应了OnMouseDown的方法。然后我们只要写了一段拦截鼠标消息的函数,在里面直接或间接调用FonMouseDown,那么就把消息和处理函数对应上去了。这里它间接调用的层数比较多,讲起来比较费时间,涉及到Message类型,建议大家去看下李维的书。




以下附上间接调用过程,其实还要很多消息发生时也间接调用了,就不一一举出来了:(


procedure WMRButtonDblClk(var Message: TWMRButtonDblClk); message WM_RBUTTONDBLCLK;//拦截消息的函数

procedure TControl.WMRButtonDblClk(var Message: TWMRButtonDblClk);

begin

inherited;

DoMouseDown(Message, mbRight, [ssDouble]);

end;


procedure DoMouseDown(var Message: TWMMouse; Button: TMouseButton;

Shift: TShiftState);

procedure TControl.DoMouseDown(var Message: TWMMouse; Button: TMouseButton;

Shift: TShiftState);

begin

if not (csNoStdEvents in ControlStyle) then

with Message do

if (Width > 32768) or (Height > 32768) then

with CalcCursorPos do

MouseDown(Button, KeysToShiftState(Keys) + Shift, X, Y)

else

MouseDown(Button, KeysToShiftState(Keys) + Shift, Message.XPos, Message.YPos);

end;


procedure MouseDown(Button: TMouseButton; Shift: TShiftState;

X, Y: Integer); dynamic;

procedure TControl.MouseDown(Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

if Assigned(FOnMouseDown) then FOnMouseDown(Self, Button, Shift, X, Y);

end;



好处:

如果你多写自己的类,你会发现这样做是多么的方便,而不会像java要写getleft,setleft,然后把text放在private中,访问和修改时要调用不同的方法,而delphi你都只是调用contol1.text来访问,control1.text:=’某字符串’来修改它的值。

而在处理消息方面,基类把onclick,onmousedown这样的属性申明为protected,如果你要使用,可以申明为pub
tobelost 2008-07-29
  • 打赏
  • 举报
回复
看下帮助嘛,自已都知道是属性
xmfan2000 2008-07-29
  • 打赏
  • 举报
回复
分表是读方法和写方法
leeky 2008-07-29
  • 打赏
  • 举报
回复
你稍微观察一下,略微想一下就知道了。
Corn1 2008-07-29
  • 打赏
  • 举报
回复
这取决于你的OOP觉悟。一般境界越高,越觉得自己写类有必要。
22222bbb 2008-07-29
  • 打赏
  • 举报
回复
感谢!就是因为自己很少写类,所以才搞不清楚.但说到这里又来了一个问题.一般在什么情况下要自己写类呢.
huangjacky 2008-07-28
  • 打赏
  • 举报
回复
property就是你的属性,然后read就是这个属性读取的值,write就是写入的。举个例子吧
private
FmyVar:integer;
....

public
function readMyVar():integer;
function writeMyVar(a:integer):integer;
.....

published

property myVar:integer read FmyVar(readMyVar) write writeMyVar;
......
22222bbb 2008-07-28
  • 打赏
  • 举报
回复
哈哈.回答问题来的啊.
yuqianyi1974 2008-07-28
  • 打赏
  • 举报
回复
property 属性名:类型 read 数据域名/方法名 write 数据域名/方法名
property为属性声明,read说明属性的读取方法,write说明属性的写方法

property FID:String read PID write PID; //fid为字符串 通过读写变量pid来设定值
etomahawk 2008-07-28
  • 打赏
  • 举报
回复
read取属性值,write设置属性的值。

另外问一下:你的两个三角怎么来的?

5,930

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧