课程表

Apex课程

工具箱
速查手册

Apex - 对象

当前位置:免费教程 » 程序设计 » Apex

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


从类创建对象

你可以在Java或其他面向对象的编程语言创建一个类对象


下面是一个名为MyClass的类的示例:

  1. //Sample Class Example
  2. public class MyClass {
  3.     Integer myInteger = 10;
  4.     public void myMethod (Integer multiplier) {
  5.         Integer multiplicationResult;
  6.         multiplicationResult=multiplier*myInteger;
  7.         System.debug('Multiplication is '+multiplicationResult);
  8.     }
  9. }

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

  1. //Object Creation
  2. //Creating an object of class
  3. MyClass objClass = new MyClass();
  4.  
  5. //Calling Class method using Class instance
  6. objClass.myMethod(100);


sObject创建

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


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

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


静态初始化

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


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

  1. //Sample Class Example with Static Method
  2. public class MyStaticClass {
  3.     Static Integer myInteger = 10;
  4.     public static void myMethod (Integer multiplier) {
  5.         Integer multiplicationResult;
  6.         multiplicationResult=multiplier*myInteger;
  7.         System.debug('Multiplication is '+multiplicationResult);
  8.     }
  9. }
  10.  
  11. //Calling the Class Method using Class Name and not using the instance object
  12. MyStaticClass.myMethod(100);

静态变量使用

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

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号