Apex開發指導--數據類型

Apex開發指導–數據類型

1,主數據類型
2,非主數據類型
3,集合類型
4,枚舉類型

主數據類型

all primitive data type arguments, such as Integer or String, are passed into methods by value. This fact means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.
所有的主數據類型的參數(例如Integer和string),在傳入方法的時候,傳入的是值,而不是地址。這個類似於C,語言的傳參,有普通的參數和指引型參數。這意味着參數的作用範圍只在方法內,當方法結束之後,參數的值就釋放了。不會去改變原來傳入的值。
所有主數據類型初始化的時候的賦值都爲 null,所以在使用的時候最好給初始化一個合適的值。防止出現異常。例如
Date d;
d.addDays(2); —這個就會報null指針異常
14中主數據類型
Blob
Boolean
Date
Datetime
String
Decimal
Double
Integer
Long
Object
ID(這個必須是18位或者15位的id,不然會報執行時異常)
Time
AnyType: valueOf 方法將一個anytype類型的sobject上的字段轉化爲標準的主數據類型,這種類型用於在追尋字段歷史記錄的時候。
Currency:Currency.newInstance靜態方法創建Currency類型的字段。此方法僅用於SOQL和SOSL WHERE子句中以過濾sObject貨幣字段。無法在任何其他類型的Apex中實例化Currency。

非主數據類型

Non-primitive data type arguments, such as sObjects, are passed into methods by reference. Therefore, when the method returns, the passed-in argument still references the same object as before the method call. Within the method, the reference can’t be changed to point to another object but the values of the object’s fields can be changed.
主數據類型的參數(例如sobject類型),這個類似於指引型傳參,就是當方法內對着個參數進行更改,那麼會影響到原來的那個參數的值。這個也類似於java裏面的包類型

集合類型

List list_name
[= new List();] |
[=new List{value [, value2. . .]};] |
;

Set set_name
[= new Set();] |
[= new Set{value [, value2. . .] };] |
;
Map<key_datatype, value_datatype> map_name
[=new map<key_datatype, value_datatype>();] |
[=new map<key_datatype, value_datatype>
{key1_value => value1_value
[, key2_value => value2_value. . .]};] |
;
Map:
1,map的key是可以爲null的
2,key值可以使用主數據類型,也可以使用非主數據類型,但是請注意,key值的和java有點不同。在saleaforce的apex開發裏面,比較的不是【對象的地址】,而是對象的值。
3,只有當key的類型爲boolean,date,datetime,decimal,double.enum,integer,id,time,string是纔可以序列化爲JSON.

枚舉類

首先說明:不同於java,apex裏面的枚舉類本身是沒有構造器語法的

定義一個枚舉類:
public enum Season {WINTER, SPRING, SUMMER, FALL}
創建了枚舉類型之後,就可以類似使用String類型一樣,可以使用它去定義任何其他的變量,方法,類

系統自帶的枚舉類
System.StatusCode 可以在使用websevice的時候,提示給請求者或者響應對方系統的狀態碼
System.LoggingLevel 用來在debug的時候指定日誌的等級

一個小知識點:
當我們定義一個變量沒有初始化的時候,他的值是null。那麼有些操作是可以的,例如下面的操作,可以思考思考哪個會發生異常,答案的話自己試試,跑跑代碼
String s;
System.assert(‘a’ == ‘A’);
System.assert(s < ‘b’);
System.assert(!(s > ‘b’));
System.assert(‘b’.compareTo(s));
System.assert(s.compareTo(‘b’));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章