国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

Apex - 類方法

2019-10-26 16:26 更新

類方法

在Apex中有兩個類方法的修飾符:Public或Protected。 返回類型是必須的方法,如果方法不返回任何東西,那么你必須提到void作為返回類型。 方法需要body。


語法:

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


語法說明:

方括號中提到的那些參數是可選的。 但是,需要以下組件:

  • return_data_type
  • method_name


類方法訪問修飾符:

使用訪問修飾符,可以為類方法指定訪問級別。 例如,公共方法將可以從類中的任何地方和類之外訪問。 私有方法只能在類中訪問。 Global將被所有Apex類訪問,并且可以作為其他頂點類訪問的Web服務方法。


例如:

//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;
}


類構造函數

構造函數是在從類藍圖創(chuàng)建對象時調用的代碼。 它與類名稱具有相同的名稱。


我們不需要為每個類定義構造函數,因為默認情況下調用無參數構造函數。 當我們想要在類初始化時完成一些變量或過程的初始化時,構造器是有用的。 例如:當調用類時,您想要將某些整數變量的值賦值為0。


例如:

//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可能有用。當創(chuàng)建類對象時,調用構造函數,如下所示:

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


重載構造函數

構造函數可以重載,即一個類可以有不止一個用不同參數定義的構造函數。


例如:

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;
}
}


您可以執(zhí)行這個類,因為我們已經在以前的例子中執(zhí)行它。

//Developer Console Code
MySampleApexClass3 objClass = new MySampleApexClass3();
Double FinalPrice = MySampleApexClass3.getCalculatedValueViaPrice(100);
System.debug('FinalPrice: '+FinalPrice);

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號