目录

Apex - Objects

类的实例称为Object。 就Salesforce而言,对象可以是类,也可以创建sObject的对象。

从类创建对象

您可以像在Java或其他面向对象的编程语言中那样创建类的对象。

以下是一个名为MyClass的示例类 -

// Sample Class Example
public class MyClass {
   Integer myInteger = 10;
   public void myMethod (Integer multiplier) {
      Integer multiplicationResult;
      multiplicationResult = multiplier*myInteger;
      System.debug('Multiplication is '+multiplicationResult);
   }
}

这是一个实例类,即,要调用或访问此类的变量或方法,您必须创建此类的实例,然后您可以执行所有操作。

// Object Creation
// Creating an object of class
MyClass objClass = new MyClass();
// Calling Class method using Class instance
objClass.myMethod(100);

sObject创建

sObjects是Salesforce的对象,您可以在其中存储数据。 例如,帐户,联系人等是自定义对象。 您可以创建这些sObject的对象实例。

以下是sObject初始化的示例,并显示了如何使用点表示法访问该特定对象的字段并将值分配给字段。

// Execute the below code in Developer console by simply pasting it
// Standard Object Initialization for Account sObject
Account objAccount = new Account(); // Object initialization
objAccount.Name = 'Testr Account'; // Assigning the value to field Name of Account
objAccount.Description = 'Test Account';
insert objAccount; // Creating record using DML
System.debug('Records Has been created '+objAccount);
// Custom sObject initialization and assignment of values to field
APEX_Customer_c objCustomer = new APEX_Customer_c ();
objCustomer.Name = 'ABC Customer';
objCustomer.APEX_Customer_Decscription_c = 'Test Description';
insert objCustomer;
System.debug('Records Has been created '+objCustomer);

静态初始化

加载类时,静态方法和变量仅初始化一次。 静态变量不会作为Visualforce页面的视图状态的一部分传输。

以下是静态方法和静态变量的示例。

// Sample Class Example with Static Method
public class MyStaticClass {
   Static Integer myInteger = 10;
   public static void myMethod (Integer multiplier) {
      Integer multiplicationResult;
      multiplicationResult = multiplier * myInteger;
      System.debug('Multiplication is '+multiplicationResult);
   }
}
// Calling the Class Method using Class Name and not using the instance object
MyStaticClass.myMethod(100);

Static Variable Use

加载类时,静态变量只会被实例化一次,这种现象可用于避免触发器递归。 静态变量值在同一执行上下文中是相同的,并且正在执行的任何类,触发器或代码都可以引用它并阻止递归。

<上一篇.Apex - Methods
Apex - Interfaces.下一篇>
↑回到顶部↑
WIKI教程 @2018