Salesforce基本变量知识(一)

基础变量知识:

salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page.

其中Apex语言和java许多的语法相似,这里总结的是一些简单的Apex的变量等知识。

有如下几种常用的基本变量Integer,String,Decimal,Double,Long,Boolean,ID。

集合常用的对象:List<T>,Set<T>,Map<T>。

时间日期常用对象:Datetime,Time,Date。

其他:Object,sObject(与数据库相关)

与JAVA一个最大的区别是:Apex中基本对象的初始值均为null。

直接上代码:

```

Integer a;

a += 1;

System.debug(a);

```

在java中此种写法是可以的,因为int类型初始值为0,a+=1以后则a变成1.但是在Apex中因为a初始值为null。
所以a+=1在运行时会抛出NullPointerException


当然,比较有意思的事情是这样,直接上代码:

```

Integer a;

System.debug(a+'1');

```

此种方法输出的结果则为null1。起始这也不奇怪,因为Apex也是基于java拓展的,如果看java编程思想了解底层的null的toString()方法处理也就知道了,当执行输出操作时,一个变量为null时,他的toString方法则返回'null'字符串。

一、基本变量:

1、Integer

Integer表示一个32位整数的对象,取值范围为-2^31 -- 2^31.

Integer主要有两个方法:

```

/*

    public String format()

    //译:将Integer值转换成String类型的值

 */

Integer goodsCount = 1;

System.debug('将Integer值转成String: ' + goodsCount.format());

/*

   public static Integer valueOf(String stringToObject)

   //译:将String类型转成Integer类型

*/

Integer goodsCount = Integer.valueOf('1');

```

2、Long

Long类型表示一个64位整数的对象,取值范围为-2^63--2^63-1.

Integer类型可以直接转换成Long类型,Long类型在不超过范围情况下可以通过intValue()方法转成Integer类型。

以下为Long类型部分主要方法:

```

Integer num = 123;

Long code = num;

//Integer类型可以直接转成Long类型

/*

   public String format()

   //译:将Long类型转换成String类型

*/

System.debug('Long类型转成String类型:' + code.format());

/*

   public Integer intValue()

   //译:将Long类型转成Integer类型

*/

   System.debug('将Long类型转成Integer类型:' + code.intValue());           

/*

   public static Long valueOf(String stringToLong)

   //译:将String类型转成Long类型

*/

Long codeLong = Long.valueOf('123');

```

3、ID

ID类型可以用任何一个符合规则的18位字符表示,如果你设置ID字符为15位,则将字符自动扩展成18位。不符合规则的ID字符在运行时则运行时异常。

以下为ID的主要方法:

```

/*

    public static ID valueOf(String toID)

    //译:将toId转换成ID

*/

String idStr = '111111111111111111';

ID id = ID.valueOf(idStr);

/*

   public Boolean equals(String id)

   //译:判断两个ID是否相同

*/

Boolean isEquals = id.equals(idStr);

```

4、Decimal

简单的来说,Decimal变量的意思为包含小数点的32位数就是Decimal,很像java中的float类型变量。

以下为Decimal的部分主要方法用来了解Decimal的功能:

```

Decimal priceDecimal = -4.50;

/*

   public Decimal abs()

   //译:返回小数点的绝对值

*/

System.debug('小数的绝对值为:' + priceDecimal.abs());

             

/*

   public Decimal divide(Decimal divisor, Integer scale)

   //译:通过divisor作为除数,并设置结果为特定的scale的位数

*/

System.debug('priceDecimal除以10小数点保留两位小数:' + priceDecimal.divide(10,2));//-0.45

            

/*

   public Double doubleValue()

   //译:将Decimal类型转换成Double类型

*/

System.debug('将priceDecimal转换成Double类型'+ priceDecimal.doubleValue());

            

/*

   public String format()

   //译:将Decimal转成String类型

*/

System.debug('Decimal转成String类型' + priceDecimal.format());

            

/*

   public Integer intValue()

   //译:将Decimal转成Integer

*/

System.debug('将Decimal转成Integer类型' + priceDecimal.intValue());

            

/*

   public Long longValue()

   //译:将Decimal转成Long类型

*/

System.debug('将Decimal转成Long类型' + priceDecimal.longValue());

            

/*

   public Decimal pow(int exponent)

   译:返回Decimal对应的指数次幂值.如果Decimal值为0,则返回1

*/

System.debug('priceDecimal平方值为:' + priceDecimal.pow(2));

            

/*

   public Integer precision()

   //译:返回Decimal值得数字的总数

*/

System.debug('priceDecimal数字总数为:'+ priceDecimal.precision());

//2 -4.5有4和5

                       

/*

   public Long round()

   //译:将Decimal值转换成最接近Long类型的值,四舍五入

*/

System.debug('priceDecimal四舍五入Long类型值为:'+ priceDecimal.round());

            

/*

   public Integer scale()

   //译:返回小数点后的位数个数

*/

System.debug('priceDecimal小数点后的位数为:' + priceDecimal.scale());

            

/*

   public Decimal setScale(Integer scale)

   //译:设置小数的小数点后的位数

*/

System.debug('设置priceDecimal的小数为2位'+ priceDecimal.setScale(2));

            

/*

   public Decimal stripTrailingZeros()

   //译:返回移除0以后的小数

*/

System.debug('移除priceDecimal小数点后的0以后的值为:'+ priceDecimal.stripTrailingZeros());

            

/*

   public String toPlainString()

   //译:返回Decimal转换后的String,Decimal值不使用科学记数法

*/

System.debug('不使用科学记数法转换成String类型'+ priceDecimal.toPlainString());

/*

   Decimal.valueOf(Object objectToDecimal)

   //译:将Object转成Decimal。其中Object可以为Double,Long,            String

*/

Long priceL = 12345;

Double priceD = 123.456;

String priceS = '12345';

Decimal d1 = Decimal.valueOf(priceL);

Decimal d2 = Decimal.valueOf(priceD);

Decimal d3 = Decimal.valueOf(priceS);

```

5、Double

Double变量为包含小数点的64位数,很像 java中的Double类型变量。

Decimal类型变量可以直接转换成Double类型变量,Double类型在不超过范围情况下可以通过以下为Double的部分主要方法:

```

Double price = 34.5678;

/*

   public static Double valueOf(String stringToDouble)

   //译:将String类型转换成Double

*/

String doubleString = '3.89';

System.debug('将字符串转换成Double' + Double.valueOf(doubleString));

         

/*

   public Long round()

   //译:返回double最接近Long的值,四舍五入

*/

Long priceLong = price.round();

System.debug('通过round方法将double转换成Long类型值为:' + priceLong);

         

/*

   public Integer intValue()

   //译:将double值转换成int类型值

*/

Integer priceInteger = price.intValue();

System.debug('将double转换成Integer类型值为:' + priceInteger);

Long priceLongByLongValue = price.longValue();

System.debug('将double转换成Long类型值为:' + priceLongByLongValue);

```

未完待续。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值