
- Apex程式設計教程
- Apex - 首頁
- Apex - 概述
- Apex - 環境
- Apex - 示例
- Apex - 資料型別
- Apex - 變數
- Apex - 字串
- Apex - 陣列
- Apex - 常量
- Apex - 決策
- Apex - 迴圈
- Apex - 集合
- Apex - 類
- Apex - 方法
- Apex - 物件
- Apex - 介面
- Apex - DML
- Apex - 資料庫方法
- Apex - SOSL
- Apex - SOQL
- Apex - 安全性
- Apex - 呼叫
- Apex - 觸發器
- Apex - 觸發器設計模式
- Apex - 限制
- Apex - 批處理
- Apex - 除錯
- Apex - 測試
- Apex - 部署
- Apex有用資源
- Apex - 快速指南
- Apex - 資源
- Apex - 討論
Apex - 物件
類的例項稱為物件。在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建立
sObject是Salesforce中儲存資料的物件。例如,Account、Contact等是自定義物件。您可以建立這些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);
靜態變數的使用
靜態變數僅在載入類時例項化一次,這種現象可以用來避免觸發器遞迴。靜態變數的值在同一執行上下文中是相同的,任何正在執行的類、觸發器或程式碼都可以引用它並防止遞迴。
廣告