教程-Tbutton(sender) 与 (sender as Tbutton) 等价吗?
问题:Tbutton(sender) 与 (sender as Tbutton) 等价吗?
答:
1. Sender As TButton时delphi做类型检查。
比如:
var frm:TForm;
beign
frm:=TFrom.Create(Applicaion);
(frm as TButton).Caption:='这是错误的';//这里delphi不允许通过
end;
2. 而TButton(Sender)不管怎么样总是试图转化的。
比如:
TButton(frm).Caption:='虽然这样可以,但是错误';//这里可以通过,但是如果类型不对,造成内存混乱。
再如:
var i:Longint;
begin
frm:=TForm.Create(Applicaiton);
i:=Longint(Frm);
TForm(i).Caption:='这样写是对的';
end;
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我