接口就像一個Apex類,其中沒有一個方法被實(shí)現(xiàn)。 它只包含方法簽名,但每個方法的主體是空的。 要使用接口,另一個類必須通過為接口中包含的所有方法提供一個體來實(shí)現(xiàn)它。
接口主要用于為代碼提供抽象層。 它們將實(shí)現(xiàn)與方法的聲明分開。
讓我們舉一個化學(xué)公司的例子。 假設(shè)我們需要向高級和普通客戶提供折扣,兩者的折扣將不同。
我們將創(chuàng)建一個接口,稱為discountprocessor。
//Interface public interface DiscountProcessor{ Double percentageDiscountTobeApplied();//method signature only } //Premium Customer Class public class PremiumCustomer implements DiscountProcessor{ //Method Call public Double percentageDiscountTobeApplied () { //For Premium customer, discount should be 30% return 0.30; } } //Normal Customer Class public class NormalCustomer implements DiscountProcessor{ //Method Call public Double percentageDiscountTobeApplied () { //For Premium customer, discount should be 10% return 0.10; } }
當(dāng)你實(shí)現(xiàn)接口,那么強(qiáng)制實(shí)現(xiàn)該接口的方法。 如果你不實(shí)現(xiàn)Interface方法,它會拋出一個錯誤。 當(dāng)您想要讓開發(fā)人員強(qiáng)制實(shí)施方法時,您應(yīng)該使用Interfaces。
SFDC有標(biāo)準(zhǔn)接口,如Database.Batchable,Schedulable等。例如,如果實(shí)現(xiàn)Database.Batchable接口,那么必須實(shí)現(xiàn)接口中定義的三個方法:開始,執(zhí)行和完成。
以下是標(biāo)準(zhǔn)Salesforce提供的Database.Batchable接口的示例,該接口向具有批處理狀態(tài)的用戶發(fā)送電子郵件。 此界面有3種方法,“開始”,“執(zhí)行”和“完成”。 使用這個接口,我們可以實(shí)現(xiàn)Batchable功能,它提供了BatchableContext變量,我們可以使用它來獲取有關(guān)正在執(zhí)行的Batch的更多信息,并執(zhí)行其他功能。
global class CustomerProessingBatch implements Database.Batchable<sobject>, Schedulable{ //Add here your email address global String [] email = new String[] {'test@test.com'}; //Start Method global Database.Querylocator start (Database.BatchableContext BC) { //This is the Query which will determine the scope of Records and fetching the same 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'); } //Execute method global void execute (Database.BatchableContext BC, List<sobject> scope) { List<apex_customer__c> customerList = new List<apex_customer__c>(); List<apex_customer__c> updtaedCustomerList = new List<apex_customer__c>(); for (sObject objScope: scope) { //type casting from generic sOject to APEX_Customer__c APEX_Customer__c newObjScope = (APEX_Customer__c)objScope ; newObjScope.APEX_Customer_Decscription__c = 'Updated Via Batch Job'; newObjScope.APEX_Customer_Status__c = 'Processed'; //Add records to the List updtaedCustomerList.add(newObjScope); } //Check if List is empty or not if (updtaedCustomerList != null && updtaedCustomerList.size()>0) { //Update the Records Database.update(updtaedCustomerList); System.debug('List Size '+updtaedCustomerList.size()); } } //Finish Method global void finish(Database.BatchableContext BC){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); //get the job Id 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()]; System.debug('$$$ Jobid is'+BC.getJobId()); //below code will send an email to User about the status mail.setToAddresses(email); //Add here your email address mail.setReplyTo('test@test.com'); mail.setSenderDisplayName('Apex Batch Processing Module'); mail.setSubject('Batch Processing '+a.Status); mail.setPlainTextBody('The Batch Apex job processed '+a.TotalJobItems+'batches with '+a.NumberOfErrors+'failures'+'Job Item processed are'+a.JobItemsProcessed); Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail}); } //Scheduler Method to scedule the class global void execute(SchedulableContext sc){ CustomerProessingBatch conInstance = new CustomerProessingBatch(); database.executebatch(conInstance,100); } }
要執(zhí)行這個類,你必須在開發(fā)者控制臺中運(yùn)行下面的代碼。
CustomerProessingBatch objBatch = new CustomerProessingBatch (); Database.executeBatch(objBatch);
更多建議: