目录

Apex - Methods

Class Methods

Apex中的类方法有两个修饰符 - 公共或受保护。 返回类型对于方法是必需的,如果方法没有返回任何内容,则必须提及void作为返回类型。 此外,方法还需要Body。

Syntax

[public | private | protected | global]
[override]
[static]
return_data_type method_name (input parameters) {
   // Method body goes here
}

语法说明

方括号中提到的那些参数是可选的。 但是,以下组件是必不可少的 -

  • return_data_type
  • method_name

类方法的访问修饰符

使用访问修饰符,您可以指定类方法的访问级别。 例如,可以从类的任何位置和类外部访问公共方法。 私有方法只能在课堂上访问。 所有Apex类都可以访问Global,并且可以将其作为其他顶点类可访问的Web服务方法公开。

Example

//Method definition and body
public static Integer getCalculatedValue () {
   //do some calculation
   myValue = myValue+10;
   return myValue;
}

此方法的返回类型为Integer,不带参数。

方法可以具有以下示例中所示的参数 -

// Method definition and body, this method takes parameter price which will then be used 
// in method.
public static Integer getCalculatedValueViaPrice (Decimal price) {
   // do some calculation
   myValue = myValue+price;
   return myValue;
}

类构造函数 (Class Constructors)

构造函数是从类蓝图创建对象时调用的代码。 它与类名具有相同的名称。

我们不需要为每个类定义构造函数,因为默认情况下会调用无参数构造函数。 构造函数对于变量的初始化或在类初始化时要完成进程很有用。 例如,您希望在调用类时将某些Integer变量的值赋值为0。

Example

// Class definition and body
public class MySampleApexClass2 {
   public static Double myValue;   // Class Member variable
   public static String myString;  // Class Member variable
   public MySampleApexClass2 () {
      myValue = 100; //initialized variable when class is called
   }
   public static Double getCalculatedValue () { // Method definition and body
      // do some calculation
      myValue = myValue+10;
      return myValue;
   }
   public static Double getCalculatedValueViaPrice (Decimal price) {
      // Method definition and body
      // do some calculation
      myValue = myValue+price; // Final Price would be 100+100=200.00
      return myValue;
   }
}

您也可以通过构造函数调用类的方法。 在为可视力控制器编程Apex时,这可能很有用。 创建类对象时,将调用构造函数,如下所示 -

// Class and constructor has been instantiated
MySampleApexClass2 objClass = new MySampleApexClass2();
Double FinalPrice = MySampleApexClass2.getCalculatedValueViaPrice(100);
System.debug('FinalPrice: '+FinalPrice);

重载构造函数

构造函数可以重载,即类可以有多个使用不同参数定义的构造函数。

Example

public class MySampleApexClass3 {  // Class definition and body
   public static Double myValue;   // Class Member variable
   public static String myString;  // Class Member variable
   public MySampleApexClass3 () {
      myValue = 100; // initialized variable when class is called
      System.debug('myValue variable with no Overaloading'+myValue);
   }
   public MySampleApexClass3 (Integer newPrice) { // Overloaded constructor
      myValue = newPrice; // initialized variable when class is called
      System.debug('myValue variable with Overaloading'+myValue);
   }
      public static Double getCalculatedValue () { // Method definition and body
      // do some calculation
      myValue = myValue+10;
      return myValue;
   }
   public static Double getCalculatedValueViaPrice (Decimal price) {
      // Method definition and body
      // do some calculation
      myValue = myValue+price;
      return myValue;
   }
}

您可以像我们在前面的示例中执行它一样执行此类。

// Developer Console Code
MySampleApexClass3 objClass = new MySampleApexClass3();
Double FinalPrice = MySampleApexClass3.getCalculatedValueViaPrice(100);
System.debug('FinalPrice: '+FinalPrice);
<上一篇.Apex - Classes
Apex - Objects.下一篇>
↑回到顶部↑
WIKI教程 @2018