目录

Apex - 数据类型

了解数据类型

Apex语言是强类型的,因此Apex中的每个变量都将使用特定的数据类型声明。 所有顶点变量最初初始化为null。 始终建议开发人员确保为变量分配正确的值。 否则,这些变量在使用时会抛出空指针异常或任何未处理的异常。

Apex支持以下数据类型 -

  • 基本类型(Primitive)(Integer, Double, Long, Date, Datetime, String, ID, or Boolean)

  • Collections(Lists, Sets and Maps)(将在第6章中介绍)

  • sObject

  • Enums

  • Classes, Objects and Interfaces(将在第11,12和13章中介绍)

在本章中,我们将查看所有原始数据类型,sObject和枚举。 我们将在接下来的章节中查看集合,Classes, Objects and Interfaces,因为它们是单独学习的关键主题。

原始数据类型

在本节中,我们将讨论Apex支持的原始数据类型。

Integer

32位数字,不包含任何小数点。 此值的范围从-2,147,483,648开始,最大值最大为2,147,483,647。

Example

我们想要申报一个变量,该变量将存储需要运送给化学加工厂买方的桶数量。

Integer barrelNumbers = 1000;
system.debug(' value of barrelNumbers variable: '+barrelNumbers);

System.debug()函数打印变量的值,以便我们可以使用它来调试或了解变量当前保持的值。

将以上代码粘贴到Developer控制台,然后单击Execute。 生成日志后,它会将变量“barrelNumbers”的值显示为1000。

Boolean

此变量可以是true,false或null。 很多时候,这种类型的变量可以用作编程中的标志,以识别是否设置了特定条件。

Example

如果要将Boolean shipmentDispatched设置为true,则可以将其声明为 -

Boolean shipmentDispatched;
shipmentDispatched = true;
System.debug('Value of shipmentDispatched '+shipmentDispatched);

Date

此变量类型表示日期。 这只能存储日期而不是时间。 为了保存日期和时间,我们需要将它存储在DateTime的变量中。

Example

请考虑以下示例以了解Date变量的工作原理。

//ShipmentDate can be stored when shipment is dispatched.
Date ShipmentDate = date.today();
System.debug('ShipmentDate '+ShipmentDate);

Long

这是一个没有小数点的64位数字。 当我们需要一系列比Integer提供的值更宽的值时,可以使用此方法。

Example

如果要存储公司收入,那么我们将使用数据类型为Long。

Long companyRevenue = 21474838973344648L;
system.debug('companyRevenue'+companyRevenue);

Object

我们可以将其称为Apex支持的任何数据类型。 例如,Class变量可以是该类的对象,而sObject泛型类型也是一个对象,类似于Account的特定对象类型也是一个Object。

Example

请考虑以下示例以了解bject变量的工作原理。

Account objAccount = new Account (Name = 'Test Chemical');
system.debug('Account value'+objAccount);

Note - 您也可以创建预定义类的对象,如下所示 -

//Class Name: MyApexClass
MyApexClass classObj = new MyApexClass();

这是将用作类变量的类对象。

String

字符串是单引号内的任何字符集。 它对字符数没有任何限制。 这里,堆大小将用于确定字符数。 这限制了Apex计划对资源的垄断,也确保了它不会变得太大。

Example

String companyName = 'Abc International';
System.debug('Value companyName variable'+companyName);

Time

此变量用于存储特定时间。 应始终使用系统静态方法声明此变量。

Blob

Blob是二进制数据的集合,存储为对象。 当我们想要将salesforce中的附件存储到变量中时,将使用此方法。 此数据类型将附件转换为单个对象。 如果要将blob转换为字符串,那么我们可以使用toString和valueOf方法。

sObject

这是Salesforce中的特殊数据类型。 它类似于SQL中的表,包含与SQL中的列类似的字段。 有两种类型的sObjects - 标准和自定义。

例如,Account是标准sObject,任何其他用户定义的对象(如我们创建的Customer对象)都是Custom sObject。

Example

//Declaring an sObject variable of type Account
Account objAccount = new Account();
//Assignment of values to fields of sObjects
objAccount.Name = 'ABC Customer';
objAccount.Description = 'Test Account';
System.debug('objAccount variable value'+objAccount);
//Declaring an sObject for custom object APEX_Invoice_c
APEX_Customer_c objCustomer = new APEX_Customer_c();
//Assigning value to fields
objCustomer.APEX_Customer_Decscription_c = 'Test Customer';
System.debug('value objCustomer'+objCustomer);

Enum

枚举是一种抽象数据类型,它存储一组有限的指定标识符的一个值。 您可以使用关键字Enum来定义枚举。 枚举可以用作Salesforce中的任何其他数据类型。

Example

您可以通过执行以下代码声明化合物的可能名称 -

//Declaring enum for Chemical Compounds
public enum Compounds {HCL, H2SO4, NACL, HG}
Compounds objC = Compounds.HCL;
System.debug('objC value: '+objC);
<上一篇.Apex - Example
Apex - 变量.下一篇>
↑回到顶部↑
WIKI教程 @2018