当前位置:K88软件开发文章中心编程语言非主流编程语言Apex → 文章内容

Apex - 变量

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:30:38

由 kaikai0220 创建,youj 最后一次修改 2016-12-12 Apex变量 Java和Apex在很多方面都是类似的。 Java和Apex中的变量声明也是相同的。 下面是一些例子来说明如何声明局部变量。 String productName = 'HCL';Integer i=0;Set<string> setOfProducts = new Set<string>();Map<id, string> mapOfProductIdToName = new Map<id, string>();请注意,所有变量都赋值为null。 声明变量您可以在Apex中声明变量,如String和Integer,如下所示: String strName = 'My String';//String variable declarationInteger myInteger = 1;//Integer variable declarationBoolean mtBoolean = true;//Boolean variable declarationApex变量不区分大小写这意味着下面的代码将会抛出一个错误,因为变量“i”已经被声明两次,并且两者将被视为相同。 Integer m = 100;for (Integer i = 0; i<10; i++) {    integer m=1; //This statement will throw an error as m is being declared again    System.debug('This code will throw error');}变量范围Apex变量从代码中声明的点开始有效。 因此,不允许在代码块中重新定义相同的变量。 此外,如果在方法中声明任何变量,那么该变量范围将仅限于该特定方法。 但是,类变量可以通过类访问。示例://Declare variable ProductsList<string> Products = new List<strings>();Products.add('HCL');//You cannot declare this variable in this code clock or sub code block again//If you do so then it will throw the error as the previous variable in scope//Below statement will throw error if declared in same code blockList<string> Products = new List<strings>();

Apex - 变量