×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex 变量


Apex变量

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

String productName = 'HCL';
Integer i=0;
Set setOfProducts = new Set();
Map mapOfProductIdToName = new Map();

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


声明变量

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

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


Apex变量不区分大小写

这意味着下面的代码将会抛出一个错误,因为变量“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 Products
List<string> Products = new List();
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 block
List<string> Products = new List();


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)