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’));

猜你喜欢

转载自blog.csdn.net/weixin_41126799/article/details/88917934