目录

Apex - 常量

与任何其他编程语言一样,常量是在声明或赋值后不会更改其值的变量。

在Apex中,当我们想要定义在整个程序执行期间应具有常量值的变量时,使用常量。 Apex常量使用关键字“final”声明。

例子 (Example)

考虑一个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;
   }
}

要查看上述类的输出,您必须在Developer Console匿名窗口中执行以下代码 -

Double finalPrice = CustomerOperationClass.provideDiscount(100);
System.debug('finalPrice '+finalPrice);
↑回到顶部↑
WIKI教程 @2018