课程表

Apex课程

工具箱
速查手册

Apex - 接口

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

接口就像一个Apex类,其中没有一个方法被实现。 它只包含方法签名,但每个方法的主体是空的。 要使用接口,另一个类必须通过为接口中包含的所有方法提供一个体来实现它。


接口主要用于为代码提供抽象层。 它们将实现与方法的声明分开。


让我们举一个化学公司的例子。 假设我们需要向高级和普通客户提供折扣,两者的折扣将不同。


我们将创建一个接口,称为discountprocessor。

  1. //Interface
  2. public interface DiscountProcessor{
  3. Double percentageDiscountTobeApplied();//method signature only
  4. }
  5.  
  6. //Premium Customer Class
  7. public class PremiumCustomer implements DiscountProcessor{
  8. //Method Call
  9. public Double percentageDiscountTobeApplied () {
  10. //For Premium customer, discount should be 30%
  11. return 0.30;
  12. }
  13. }
  14.  
  15. //Normal Customer Class
  16. public class NormalCustomer implements DiscountProcessor{
  17. //Method Call
  18. public Double percentageDiscountTobeApplied () {
  19. //For Premium customer, discount should be 10%
  20. return 0.10;
  21. }
  22. }

当你实现接口,那么强制实现该接口的方法。 如果你不实现Interface方法,它会抛出一个错误。 当您想要让开发人员强制实施方法时,您应该使用Interfaces。


批处理的标准Salesforce接口

SFDC有标准接口,如Database.Batchable,Schedulable等。例如,如果实现Database.Batchable接口,那么必须实现接口中定义的三个方法:开始,执行和完成。


以下是标准Salesforce提供的Database.Batchable接口的示例,该接口向具有批处理状态的用户发送电子邮件。 此界面有3种方法,“开始”,“执行”和“完成”。 使用这个接口,我们可以实现Batchable功能,它提供了BatchableContext变量,我们可以使用它来获取有关正在执行的Batch的更多信息,并执行其他功能。

  1. global class CustomerProessingBatch implements Database.Batchable<sobject>, Schedulable{
  2. //Add here your email address
  3. global String [] email = new String[] {'test@test.com'};
  4.   
  5. //Start Method
  6. global Database.Querylocator start (Database.BatchableContext BC) {
  7.     //This is the Query which will determine the scope of Records and fetching the same
  8.     return Database.getQueryLocator('Select id, Name, APEX_Customer_Status__c, APEX_Customer_Decscription__c From APEX_Customer__c WHERE createdDate = today && APEX_Active__c = true');
  9. }
  10.  
  11. //Execute method
  12. global void execute (Database.BatchableContext BC, List<sobject> scope) {
  13.     List<apex_customer__c> customerList = new List<apex_customer__c>();
  14.     List<apex_customer__c> updtaedCustomerList = new List<apex_customer__c>();
  15.     for (sObject objScope: scope) {
  16.    //type casting from generic sOject to APEX_Customer__c
  17.         APEX_Customer__c newObjScope = (APEX_Customer__c)objScope ;
  18.         newObjScope.APEX_Customer_Decscription__c = 'Updated Via Batch Job';
  19.         newObjScope.APEX_Customer_Status__c = 'Processed';
  20.    //Add records to the List
  21.         updtaedCustomerList.add(newObjScope);
  22.     }
  23.     
  24.     //Check if List is empty or not
  25.     if (updtaedCustomerList != null && updtaedCustomerList.size()>0) {
  26. //Update the Records
  27.         Database.update(updtaedCustomerList); System.debug('List Size '+updtaedCustomerList.size());
  28.     }
  29. }
  30.  
  31. //Finish Method
  32. global void finish(Database.BatchableContext BC){
  33.     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  34.    
  35.     //get the job Id
  36.     AsyncApexJob a = [Select a.TotalJobItems, a.Status, a.NumberOfErrors, a.JobType, a.JobItemsProcessed, a.ExtendedStatus, a.CreatedById, a.CompletedDate From AsyncApexJob a WHERE id = :BC.getJobId()];
  37.     System.debug('$$$ Jobid is'+BC.getJobId());
  38.     
  39.     //below code will send an email to User about the status
  40.     mail.setToAddresses(email);
  41.     
  42.     //Add here your email address
  43.     mail.setReplyTo('test@test.com');
  44.     mail.setSenderDisplayName('Apex Batch Processing Module');
  45.     mail.setSubject('Batch Processing '+a.Status);
  46.     mail.setPlainTextBody('The Batch Apex job processed  '+a.TotalJobItems+'batches with  '+a.NumberOfErrors+'failures'+'Job Item processed are'+a.JobItemsProcessed);
  47.   
  48.     Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail});
  49. }
  50.  
  51. //Scheduler Method to scedule the class
  52. global void execute(SchedulableContext sc){
  53.     CustomerProessingBatch conInstance = new CustomerProessingBatch();
  54.     database.executebatch(conInstance,100);
  55. }
  56. }


要执行这个类,你必须在开发者控制台中运行下面的代码。

  1. CustomerProessingBatch objBatch = new CustomerProessingBatch ();
  2. Database.executeBatch(objBatch);
转载本站内容时,请务必注明来自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号