课程表

Apex课程

工具箱
速查手册

Apex - 类方法

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

在Apex中有两个类方法的修饰符:Public或Protected。 返回类型是必须的方法,如果方法不返回任何东西,那么你必须提到void作为返回类型。 方法需要body。


语法:

  1. [public | private | protected | global] 
  2. [override] 
  3. [static] 
  4. return_data_type method_name (input parameters) 
  5. {
  6. // Method body goes here
  7. }


语法说明:

方括号中提到的那些参数是可选的。 但是,需要以下组件:

  • return_data_type
  • method_name


类方法访问修饰符:

使用访问修饰符,可以为类方法指定访问级别。 例如,公共方法将可以从类中的任何地方和类之外访问。 私有方法只能在类中访问。 Global将被所有Apex类访问,并且可以作为其他顶点类访问的Web服务方法。


例如:

  1. //Method definition and body
  2. public static Integer getCalculatedValue () {
  3.     //do some calculation
  4.     myValue = myValue+10;
  5.     return myValue;
  6. }

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

方法可以具有如下面示例所示的参数:

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


类构造函数

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


我们不需要为每个类定义构造函数,因为默认情况下调用无参数构造函数。 当我们想要在类初始化时完成一些变量或过程的初始化时,构造器是有用的。 例如:当调用类时,您想要将某些整数变量的值赋值为0。


例如:

  1. //Class definition and body
  2. public class MySampleApexClass2 {
  3. public static Double myValue;   //Class Member variable
  4. public static String myString;  //Class Member variable
  5.     
  6. public MySampleApexClass2 () {
  7.     myValue = 100;  //initialized variable when class is called
  8.         
  9. }
  10.     
  11. public static Double getCalculatedValue () {    //Method definition and body
  12.     //do some calculation
  13.     myValue = myValue+10;
  14.     return myValue;
  15. }
  16.     
  17. public static Double getCalculatedValueViaPrice (Decimal price) {   //Method definition and body
  18.     //do some calculation
  19.     myValue = myValue+price;//Final Price would be 100+100=200.00
  20.     return myValue;
  21. }
  22. }


你也可以通过构造函数调用类的方法。 视觉控制器编程Apex可能有用当创建类对象时,调用构造函数,如下所示:

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


重载构造函数

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


例如:

  1. public class MySampleApexClass3 {   //Class definition and body
  2. public static Double myValue;       //Class Member variable
  3. public static String myString;      //Class Member variable
  4.     
  5. public MySampleApexClass3 () {
  6.     myValue = 100;  //initialized variable when class is called
  7.     System.debug('myValue  variable with no Overaloading'+myValue);
  8. }
  9.     
  10. public MySampleApexClass3 (Integer newPrice) {  //Overloaded constructor
  11.     myValue = newPrice; //initialized variable when class is called
  12.     System.debug('myValue  variable with Overaloading'+myValue);
  13. }
  14.     
  15. public static Double getCalculatedValue () {    //Method definition and body
  16.     //do some calculation
  17.     myValue = myValue+10;
  18.     return myValue;
  19. }
  20.     
  21.     
  22. public static Double getCalculatedValueViaPrice (Decimal price) {   //Method definition and body
  23.     //do some calculation
  24.     myValue = myValue+price;
  25.     return myValue;
  26. }
  27. }


您可以执行这个类,因为我们已经在以前的例子中执行它。

  1. //Developer Console Code
  2. MySampleApexClass3 objClass = new MySampleApexClass3();
  3. Double FinalPrice = MySampleApexClass3.getCalculatedValueViaPrice(100);
  4. System.debug('FinalPrice: '+FinalPrice);
转载本站内容时,请务必注明来自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号