课程表

Apex课程

工具箱
速查手册

Apex - 变量

当前位置:免费教程 » 程序设计 » Apex

Apex变量

Java和Apex在很多方面都是类似的。 Java和Apex中的变量声明也是相同的。 下面是一些例子来说明如何声明局部变量。

  1. String productName = 'HCL';
  2. Integer i=0;
  3. Set<string> setOfProducts = new Set<string>();
  4. Map<id, string> mapOfProductIdToName = new Map<id, string>();

请注意,所有变量都赋值为null。


声明变量

您可以在Apex中声明变量,如String和Integer,如下所示:

  1. String strName = 'My String';//String variable declaration
  2. Integer myInteger = 1;//Integer variable declaration
  3. Boolean mtBoolean = true;//Boolean variable declaration


Apex变量不区分大小写

这意味着下面的代码将会抛出一个错误,因为变量“i”已经被声明两次,并且两者将被视为相同。

  1. Integer m = 100;
  2. for (Integer i = 0; i<10; i++) {
  3.     integer m=1; //This statement will throw an error as m is being declared again
  4.     System.debug('This code will throw error');
  5. }


变量范围

Apex变量从代码中声明的点开始有效。 因此,不允许在代码块中重新定义相同的变量。 此外,如果在方法中声明任何变量,那么该变量范围将仅限于该特定方法。 但是,类变量可以通过类访问。

示例:
  1. //Declare variable Products
  2. List<string> Products = new List<strings>();
  3. Products.add('HCL');
  4. //You cannot declare this variable in this code clock or sub code block again
  5. //If you do so then it will throw the error as the previous variable in scope
  6. //Below statement will throw error if declared in same code block
  7. List<string> Products = new List<strings>();
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号