- 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);
静态变量的使用
静态变量仅在加载类时实例化一次,这种现象可以用来避免触发器递归。静态变量的值在同一执行上下文中是相同的,任何正在执行的类、触发器或代码都可以引用它并防止递归。
广告