Apex - 常量



如在其他任何程式語言一樣,常量是指宣告或賦值後其值不會改變的變數。

在 Apex 中,常量用於定義在整個程式執行期間應具有常量值的變數。Apex 常量使用關鍵字“final”宣告。

示例

考慮一個 CustomerOperationClass 類和其中的常量變數 regularCustomerDiscount

public class CustomerOperationClass {
   static final Double regularCustomerDiscount = 0.1;
   static Double finalPrice = 0;
   
   public static Double provideDiscount (Integer price) {
      //calculate the discount
      finalPrice = price - price * regularCustomerDiscount;
      return finalPrice;
   }
}

要檢視上述類的輸出,您必須在“開發人員控制檯匿名視窗”中執行以下程式碼 −

Double finalPrice = CustomerOperationClass.provideDiscount(100);
System.debug('finalPrice '+finalPrice);
廣告