本文整理自网络,侵删。
简单一句话:Delphi 终于不用非将变量定义在函数的开头位置了。
拓展开来:
1、您可以在代码中使用变量时,通过下面的格式来定义变量:
var 变量名[:变量类型][:=变量值]
示例一:
procedureTest;begin varI,J:Integer; I:=22; j:=I+20; ShowMessage(J.ToString);end;示例2:
procedureTest;// declaration and initialization in a single statementbegin varI:Integer:=22; ShowMessage(I.ToString);end;示例3:
procedureTest1;// multiple inline declarations (symbols declared when used)begin varI:Integer:=22; varJ:Integer:=22+I; varK:Integer:=I+J; ShowMessage(K.ToString);end;2、内联变量的生命周期和C++一样,仅在方寸之间,如 begin/end 之间,循环体内等。
3、如果不指类变量的类型,会根据赋值的类型进行推断,相当于 C++ 的 auto 类型。
4、常量也可以内联了,只不过将 var 换成 const ,其它一致。
简译自 Macro 技术博客
相关阅读 >>
Delphi twebbrowser:确定带有框架的页面何时完成
Delphi firedac tfdconnection连接mysql数据库
Delphi 实现dns上线域名解析(用于远控server)
Delphi sysutils.stringreplace - 替换
更多相关阅读请进入《Delphi》频道 >>