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。

eg:

1
2
3
Integer i;
i += 1;
System.debug(i);
1
 

在java中此种写法是可以的,因为int类型初始值为0,i+=1以后则i变成1.但是在Apex中因为i初始值为null。
所以i+=1在运行时会抛出NullPointerException
当然,比较有意思的事情是这样,直接上代码:

1
2
Integer i;
System.debug(i+'1');

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

一)基本变量:

1)Integer

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

Integer主要有两个方法:

1
2
3
4
5
6
7
8
9
10
11
/*
    public String format()
    //译:将Integer值转换成String类型的值
 */
Integer goodsCount = 12;
System.debug('将Integer值转成String: ' + goodsCount.format());
/*
   public static Integer valueOf(String stringToObject)
   //译:将String类型转成Integer类型
*/
Integer goodsCountI = Integer.valueOf('12');

2)Long

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Integer transferSource = 12345;<br>Long code = transferSource;//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的主要方法:

1
2
3
4
5
6
7
8
9
10
11
/*
    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 function

5)Double

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

Decimal类型变量可以直接转换成Double类型变量,Double类型在不超过范围情况下可以通过

以下为Double的部分主要方法:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);

6)String

String类型和Java中的String类型很类似,在这里不做过多解释,代码中主要需要看一下String类型对象和上述变量如何相互转换,这在项目中是经常用到的,也是必须需要知道的。以下为String类型主要方法:

 String goodsName = 'abcd123汉字显示';//测试文本
/*
public String abbreviate(Integer maxWidth)
//译:返回简化字符串,maxWidth>自身长度?自身:maxWidth长度的字符串加上省略号,省略号占3个字符
//注意:maxWidth如果小于4,则抛出Runtime Exception
*/ System.debug('简化后的字符串名称为: '+goodsName.abbreviate(5));//结果:ab... /*
public String abbreviate(Integer maxWidth,Integer offset)
//译:返回简化字符串,maxWidth为需要简化长度,offset为字符串简化起点
//如果max太小,则抛出Runtime Exception
*/
//返回简化字符串,参数1:最大长度;参数2:偏移量offset
System.debug('简化并添加偏移量的字符串名称为:'+goodsName.abbreviate(5,2)); /*
public String capitalize()
//译:返回当前字符串,其中第一个字母改为标题(大写)。
*/ System.debug('将首字母大写显示'+goodsName.capitalize()); /*
public String center(Integer size)
//译:返回指定大小字符串使原字符串位于中间位置(空格填充左右),如果size<字符串长度,则不起作用
*/ System.debug('设置指定字符串长度为20的显示为:' + goodsName.center(20)); /*
public String center(Integer size,String paddingString)
//译:返回指定大小字符串使原字符串处于中间位置。参数1:字符串显示长度;参数2:填充的字符样式
*/
//返回值:----abcd123汉字显示-----
System.debug('使用-填充字符串显示为:'+goodsName.center(20,'-')); /*
public Integer charAt(Integer index)
//译:返回对应值得ASC码值
*/
System.debug('goodsName.charAt(5)' + goodsName.charAt(5)); /*
public Integer codePointAt(Integer index)
//译:返回指定位置的值对应的Unicode编码
*/ System.debug('goodsName.codePoint(5)' + goodsName.codePointAt(5)); /*
public Integer codePointBefore(Integer index)
//译:返回指定位置的值前一个对应的Unicode编码
*/
System.debug('goodsName.codePointBefore(5)' + goodsName.codePointBefore(5)); /*
public Integer codePointCount(Integer beginIndex,Integer endIndex)
//译:返回起始位置到截至位置字符串的Unicode编码值
*/ System.debug('goodsName.codePointCount(5,7)' + goodsName.codePointCount(5,7)); /*
public Integer compareTo(String secondString)
//译:基于Unicode比较两个字符串大小,如果小于比较值返回负整数,大于返回正整数,等于返回0
*/
System.debug('两个字符串比较的情况为 : ' + goodsName.compareTo('compareString')); /*
public Boolean contains(String substring)
//译:判断是否包含某个字符串,包含返回true,不包含返回false
*/ System.debug('商品名称是否包含abcd : ' + goodsName.contains('abcd')); /*
public Boolean containsAny(String inputString)
//译:判断是否包含inputString任意一个字符,包含返回true,不包含返回false
*/
System.debug('商品名称是否包含abcd任意一个字符:' + goodsName.containsAny('abcd')); /*
public Boolean containsIgnoreCase(String inputString)
//译:判断是否包含inputString(不区分大小写),包含返回true,不包含返回false
*/ System.debug('商品名称是否包含AbCd(不区分大小写:)' + goodsName.containsIgnoreCase('AbCd'));
/*
public Boolean containsNone(String inputString)
//译:判断是否不包含inputString,不包含返回true,包含返回false
*/
System.debug('商品名称是否不包含abcd'+goodsName.containsNone('abcd')); /*
public Boolean containsOnly(String inputString)
//译:当前字符串从指定序列只包括inputString返回true,否则返回false
*/
System.debug('商品名称是否只包含abcd:'+ goodsName.containsOnly('abcd')); /*
public Boolean containsWhitespace()
//译:判断字符串是否包含空格,包含返回true,不包含返回false
*/
System.debug('商品名称是否包含空格 : ' + goodsName.containsWhitespace()); /*
public Integer countMatches(String substring)
//译:判断子字符串在字符串中出现的次数
*/
System.debug('商品名称出现abcd的次数:' + goodsName.countMatches('abcd')); /*
public String deleteWhitespace()
//译:移除字符串中所有的空格
*/
String removeWhitespaceString = ' a b c d ';
System.debug('原 a b c d ,移除空格的字符串显示为:' + removeWhitespaceString.deleteWhitespace()); /*
public String difference(String anotherString)
//译:返回两个字符串之间不同,如果anotherString为空字符串,则返回空字符串,如果anotherString为null,则抛异常
//比较结果以anotherString为基准,从第一个字符比较,不相同则返回anotherString与源字符串不同之处
*/
//返回值:bcd啦啦啦
System.debug('商品名称和abcd啦啦啦的不同返回值为:' + goodsName.difference('bcd啦啦啦')); /*
public Boolean endsWith(String substring)
//译:判断字符串是否已substring截止,如果是返回true,否则返回false
*/
System.debug('商品名称是否已 显示 截止 :' + goodsName.endsWith('显示')); /*
public Boolean endsWithIgnoreCase(String substring)
//译:判断字符串是否已substring截止(不区分大小写),如果是返回true,否则返回false
*/
System.debug('商品名称是否已 显示 截止(不区分大小写) :' + goodsName.endsWithIgnoreCase('显示')); /*
public Boolean equals(Object anotherString)
//译:判断字符串是否和其他字符串相同
*/ /*
public Boolean equalsIgnoreCase(String anotherString)
//译:判断字符串是否和其他字符串相同(不区分大小写)
*/ String testEquals = 'AbCd123汉字显示'; System.debug('商品名称是否和testEquals字符串相同:' + goodsName.equals(testEquals)); System.debug('商品名称是否和testEquals字符串相同:(不区分大小写)'+goodsName.equalsIgnoreCase(testEquals)); /*
public static String format(String stringToFormat, List<String> formattingArguments)
//译:将转换的参数替换成相同方式的字符串中
*/
String sourceString = 'Hello {0} ,{1} is good';
List<String> formattingArguments = new String[]{'Zero','Apex'};
System.debug('替换sourceString内容以后显示为:' + String.format(sourceString,formattingArguments)); /*
public static String fromCharArray(List<Integer> charArray)
//译:将char类型数组转换成String类型
*/
List<Integer> charArray = new Integer[] {55,57};
String destinatioin = String.fromCharArray(charArray);
System.debug('通过fromCharArray方法转换的字符串为:' + destinatioin); /*
public List<Integer> getChars()
//译:返回字符串的字符列表
*/
List<Integer> goodsNameChars = goodsName.getChars(); /*
public static String getCommonPrefix(List<String> strings)
//译:获取列表共有前缀
*/
List<String> strings = new String[]{'abcdf','abe'};
String commonString = String.getCommonPrefix(strings);
System.debug('共有前缀:' + commonString); //goodsName.getLevenshteinDistance();//待学习 /*
public Integer hashCode()
//译:返回字符串的哈希值
*/
Integer hashCode = goodsName.hashCode();
System.debug('商品名称的哈希值为:'+hashCode);
/*
public Integer indexOf(String substring)
//译:返回substring第一次在字符串中出现的位置,如果不存在返回-1
*/
System.debug('cd 在商品名称中出现的位置:' + goodsName.indexOf('cd')); /*
public Integer indexOf(String substring,Integer index)
//译:返回substring第一次在字符串中出现的位置,起始查询字符串的位置为index处
*/
System.debug('cd 在商品名称中出现的位置:' + goodsName.indexOf('cd',2)); /*
public Integer indexOfAny(String substring)
//译:substring任意一个字符第一次在字符串中出现的位置
*/
System.debug('商品信息中select任意字符最先出现位置:' + goodsName.indexOfAny('select')); /*
public Integer indexOfAnyBut(String substring)
//译:返回substring任意一个字符不被包含的第一个位置,无则返回-1
*/
System.debug('商品信息中select任意一个字符最先不被包含的位置'+goodsName.indexOfAnyBut('select')); /*
public Integer indexOfChar(int char)
//译:返回字符串中char字符最先出现的位置
*/
Integer firstChar = goodsName.indexOfChar(55); /*
public Integer indexOfDifference(String compareTo)
//译:返回两个字符串第一个不同位置的坐标
*/
System.debug('商品名称与abce字符串第一个不同的位置为:' + goodsName.indexOfDifference('abce')); /*
public Integer indexOfIgnoreCase(String substring)
//译:返回substring在字符串中第一个出现的位置(不考虑大小写)
*/
System.debug('商品名称中第一个出现CD位置的为(不分大小写)' + goodsName.indexOfIgnoreCase('CD')); /*
public Boolean isAllLowerCase()
//译:字符串是否均为小写,如果是返回true,否则返回false
*/
System.debug('商品名称中是否全是小写: ' + goodsName.isAllLowerCase()); /*
public Boolean isAllUpperCase()
//译:字符串是否均为大写,如果是返回true,否则返回false
*/
System.debug('商品名称中是否全是大写:' + goodsName.isAllUpperCase()); /*
public Boolean isAlpha()
//译:如果当前所有字符均为Unicode编码,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode编码:' + goodsName.isAlpha()); /*
public Boolean isAlphanumeric()
//译:如果当前所有字符均为Unicode编码或者Number类型编码,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode或者Number类型编码:' + goodsName.isAlphanumeric()); /*
public Boolean isAlphanumericSpace()
//译:如果当前所有字符均为Unicode编码或者Number类型或者空格,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode,Number或者空格' + goodsName.isAlphanumericSpace()); /*
public Boolean isAlphaSpace()
//译:如果当前所有字符均为Unicode或者空格,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode,或者空格' +goodsName.isAlphaSpace()); /*
public Boolean isAsciiPrintable()
//译:如果当前所有字符均为可打印的Asc码,则返回true,否则返回false
*/
System.debug('商品名称所有字符是否均为可打印的Asc码:' + goodsName.isAsciiPrintable()); /*
public Boolean isNumeric()
//译:如果当前字符串只包含Unicode的位数,则返回true,否则返回false
*/
System.debug('商品名称所有字符是否均为Unicode的位数:' + goodsName.isNumeric()); /*
public Boolean isWhitespace()
//译:如果当前字符只包括空字符或者空,则返回true,否则返回false
*/
System.debug('商品名称所有字符只包括空字符或者空:' + goodsName.isWhitespace()); /*
public static String join(Object iterableObj, String separator)
//译:通过separator连接对象,通用于数组,列表等
*/
List<Integer> intLists = new Integer[] {1,2,3};
String s = String.join(intLists,'/');//s = 1/2/3 /*
public Boolean lastIndexOf(String substring)
//译:substring在字符串中最后出现的位置,不存在则返回-1
*/
System.debug('cd最后一次出现的位置:' + goodsName.lastIndexOf('cd'));
/*
public Boolean lastIndexOfIgnoreCase(String substring)
//译:substring在字符串中最后出现的位置(忽略大小写),不存在则返回-1
*/
System.debug('Cd最后一次出现的位置(不考虑大小写):' + goodsName.lastIndexOfIgnoreCase('Cd')); /*
public String left(Integer index)
//译:获取从零开始到index处的字符串 */
System.debug('商品名称前三个字符 : abc' + goodsName.left(3)); /*
public String leftPad(Integer length)
//译:返回当前字符串填充的空间左边和指定的长度
*/
String s1 = 'ab';
String s2 = s1.leftPad(4);//s2 = ' ab';
/*
public Integer length()
//译:返回字符串长度
*/
System.debug('商品名称长度:' + goodsName.length());
/*
public String mid(Integer startIndex,Integer length);
//译:返回新的字符串,第一个参数为起始位,第二个字符为字符的长度//类似于substring
*/
System.debug('商品名称截取字符串' + goodsName.mid(2,3)); /*
public String normalizeSpace()
//译:前后删除空白字符
*/
String testNormalizeSpace = 'abc\t\n de';
System.debug('通过normalizeSpace处理后的字符串为:' + testNormalizeSpace.normalizeSpace()); /*
public String remove(String substring)
//译:移除所有特定的子字符串,并返回新字符串
public String removeIgnorecase(String substring)
//译:移除所有特定的子字符串(忽略大小写),并返回新字符串
*/
System.debug('商品名称移除12后的字符串为:' + goodsName.remove('')); /*
public String removeEnd(String substring)
//译:当且仅当子字符串在后面移除子字符串
*/
System.debug('当显示在商品名称最后时移除:' + goodsName.removeEnd('显示')); /*
public String removeStatrt(String substring)
//译:当且仅当子字符串在后面移除子字符串
*/
System.debug('当ab在商品名称最前面时移除' + goodsName.removeStart('ab')); /*
public String repeat(Integer numberOfTimes)
//译:重复字符串numberOfTimes次数
*/
System.debug('重复商品名称两次的显示为:' + goodsName.repeat(2)); /*
public String repeat(String separator, Integer numberOfTimes)
//译:重复字符串numberOfTimes次,通过separator作为分隔符
*/
System.debug('通过separator分隔符重复字符串两次:' + goodsName.repeat('-',2)); /*
public String replace(String target, String replacement)
//译:将字符串中的target转换成replacement
*/
System.debug('将商品名称中ab替换成AB' + goodsName.replace('ab','AB')); /*
public String replaceAll(String target,String replacement)
*/
System.debug('将商品名称中ab全部替换成AB' + goodsName.replaceAll('ab','AB')); /*
public String reverse()
//译:字符串倒序排列
*/
System.debug('商品名称倒序:' + goodsName.reverse()); /*
public String right(Integer length)
//译:从右查询返回length的个数的字符串
*/
System.debug('返回商品名称后三位字符:' + goodsName.right(3)); /*
public String[] split(String regExp)
//译:通过regExp作为分隔符将字符串分割成数组
*/
String[] sonSplitString = goodsName.split('');//通过1分割字符串 /*
public String[] split(String regExp, Integer limit)
//译:通过regExp作为分隔符将字符串分割成数组,limit显示数组个数
*/
String [] sonSplitString1 = goodsName.split('',2); /*
public Boolean startsWith(string substring)
//译:判断字符串是否以substring开头,如果是返回true,不是返回false
*/
System.debug('商品名称是否以abcd开始:' + goodsName.startsWith('abcd')); /*
public String substring(Integer length)
//译:截取字符串固定长度
*/
System.debug('截取商品名称前四个字符: ' + goodsName.substring(4)); /*
public String toLowerCase()
//译:将字符串转换成小写
*/
System.debug('商品名称转换成小写:' + goodsName.toLowerCase()); /*
public String toUpperCase()
//译:将字符串转换成大写
*/
System.debug('商品名称转换成大写:' + goodsName.toUpperCase()); /*
public String trim()
//译:去字符串左右空格
*/
System.debug('去空格后商品名称:' + goodsName.trim()); /*
public String uncapitalize()
//译:将字符串第一个转换成小写
*/
System.debug('商品名称第一个单词转换成小写:' + goodsName.uncapitalize()); /*
public static String valueOf(Object objectToConvert)
//译:将Object类型转换成String类型,其中Object类型包括以下:
// Date,DateTime,Decimal,Double,Integer,Long,Object
*/
System.debug('将Double类型转换成String类型' + String.valueOf(3.45)); String Function

7)Boolean

Boolean类型声明一个布尔类型,和java区别为:Boolean类型变量有三个取值:true,false,null(default),所以使用Boolean类型声明的时候必须赋予初始值,否则初始值为null

 二)时间日期类型

1)Datetime

Datetime类型声明一个日期时间的对象,包含两部分:日期,时间。因为salesforce一般制作global项目,所以日期时间一般取格林时间。Datetime无构造函数,如果实例化只能通过其静态方法初始化。以下为Datetime的部分主要方法:

 Datetime nowDatetime = Datetime.now();
Datetime datetime1 = Datetime.newInstance(2015,3,1,13,26,0);
String datetimeString = '2016-3-1 PM14:38';
/*
Datetime datetime2 = Datetime.parse(datetimeString);
Datetime datetime3 = Datetime.valueOf(datetimeString);
*/
System.debug('通过初始化年月日时分秒得到的Datetime,并转换格式值:'+datetime1.format('yyyy-MM-dd HH:mm:ss'));
System.debug('当前日期时间:' + nowDatetime.format());
/*
System.debug('通过parse方法初始化的datetime:' + datetime2.format());
System.debug('通过valueOf方法初始化的datetime'+datetime3.format());
*/
datetime1 = datetime1.addDays(1);
datetime1 = datetime1.addMonths(1);
datetime1 = datetime1.addYears(1);
datetime1 = datetime1.addHours(1);
datetime1 = datetime1.addMinutes(1);
datetime1 = datetime1.addSeconds(1);
System.debug('更改后的日期时间:' + datetime1.format('yyyy-MM-dd HH:mm:ss'));
Date date1 = datetime1.date();
System.debug('datetime1对应的Date为:'+date1.format()); Date dateGmt = datetime1.dateGmt();
System.debug('datetime1对应的DateGmt值为:'+dateGmt.format());
Integer year = datetime1.year();
Integer yearGmt = datetime1.yearGmt();
Integer month = datetime1.month();
Integer monthGmt = datetime1.monthGmt();
Integer day = datetime1.day();
Integer dayGmt = datetime1.dayGmt();
Integer dayOfYear = datetime1.dayOfYear();
Integer dayOfYearGmt = datetime1.dayOfYearGmt();
Integer hour = datetime1.hour();
Integer hourGmt = datetime1.hourGmt();
Integer minute = datetime1.minute();
Integer minuteGmt = datetime1.minuteGmt();
Integer second = datetime1.second();
Integer secondGmt = datetime1.secondGmt();
System.debug('year : '+ year + '\tyearGmt : ' + yearGmt);
System.debug('month : ' + month + '\tmonthGmt : '+ monthGmt);
System.debug('day : ' + day + '\tdayGmt : ' + dayGmt);
System.debug('hour : ' + hour + '\thourGmt : ' + hourGmt);//两者不同 一个为14 Gmt为6
System.debug('minute : ' + minute + '\tminuteGmt : ' + minuteGmt);
System.debug('second : ' + second + '\tsecondGmt : ' + secondGmt);
System.debug('dayOfYear : ' + dayOfYear + '\tdayOfYearGmt : ' + dayOfYearGmt);
System.debug('转成本地日期并以长日期类型显示:'+ datetime1.formatLong());
Long timeL = datetime1.getTime();
System.debug('转成time类型的Long类型显示为:'+timeL.format());
Datetime datetime5 = Datetime.newInstance(2016,4,2);
System.debug('datetime1与datetime2是否同一天:' + datetime1.isSameDay(datetime5));//true Datetime function

2)Date

Date类型声明一个日期的对象,Date可以和Datetime相互转换,主要需要掌握二者关系以及相互转换。

以下为Date部分主要方法:

 Date date2 = Date.today();
System.debug('当前日期:' + date2.format());
Date date3 = Date.newInstance(2016,3,1);
String dateString = '2016-3-1';
Date date4 = Date.parse(dateString);
Date date5 = Date.valueOf(dateString);
System.debug('通过newInstance实例化:' + date3.format());
System.debug('通过parse实例化:' + date4.format());
System.debug('通过valueOf实例化:' + date5.format()); date3 = date3.addMonths(1);
date3 = date3.addDays(1);
System.debug('date3的日期为:' + date3.format());
Integer year1 = date3.year();
Integer month1 = date3.month();
Integer day1 = date3.day();
System.debug('year : ' + year1);
System.debug('month : ' + month1);
System.debug('day : ' + day1);
Integer dayOfYear1 = date3.dayOfYear();
System.debug('dayOfYear : ' + dayOfYear1); Integer daysBetween = date3.daysBetween(date4);//date4-date3
System.debug('date3和date4相差天数:' + daysBetween); System.debug('date4和date5是否相同日期:'+date4.isSameDay(date5)); System.debug('date3和date4相差月数:' + date3.monthsBetween(date4)); System.debug('调用toStartOfMonth执行值:' + date3.toStartOfMonth().format());//返回本月第一天
/*
public Date toStartOfWeek()
//译:返回本月第一个周日,如果本月1日非周日,则返回上月最晚的周日
*/
System.debug('调用toStartOfWeek执行值: ' + date3.toStartOfWeek().format()); Date function

3)Time

Time类型声明一个时间的对象,对于时间需要考虑的是:因为中国时间和格林时间相差8小时,所以具体项目时如果是global项目需要考虑使用格林时间,即GMT时间。

三)集合类型 

集合类型主要有三种,List,Set以及Map。其中三种均为泛型方式,所以声明变量时,直接带上泛型。

1)List<T> 

List代表一类的有序数据列表。数据序号从0开始。与JAVA不同的是:List是一个类,并且不存在ArrayList等子类。即实例化

eg:List<String> list1 = new List<String>();

List可以通过自身构造函数实例化,也可以通过数组进行实例化。

以下为List主要方法:

注:set()方法在设置插入位置以前应确保长度大于需要插入的位置,否则将抛出异常。

 List<String> lists = new String[]{'',''};
List<String> list1 = new String[] {'',''};
lists.set(0,'a');
lists.add(0,'b');
lists.add('');
lists.addAll(list1);
//lists.sort();
for(String item : lists) {
System.debug('item : ' + item);
}
Iterator<String> iterator = lists.iterator();
while(iterator.hasNext()) {
String item = iterator.next();
System.debug('通过iterator遍历:'+ item);
}
if(lists.size() > 0) {
Integer listSize = lists.size();
lists.remove(listSize-1);
}
List<String> cloneList = lists.clone();
for(Integer i=0;i<cloneList.size();i++) {
System.debug('cloneListItem : ' + cloneList.get(i));
}
lists.clear(); if(lists.size() > 0) {
for(String item : lists) {
System.debug('set item : ' + item);
}
} else {
System.debug('lists has already clear');
} List Function

2)Set<T>

Set代表一类数据的无序列表。与JAVA不同的是:Set是一个类,不存在HashSet等子类。即实例化

eg:Set<String> set1 = new Set<String>();

Set主要方法如下:

 Set Function

3)Map<K,V>

Map代表着键值对,与JAVA用法类似,区别为Map是一个类,不是接口,不存在HashMap<K,V>等子类

Map主要方法如下:

 Map<String,Object> map1 = new Map<String,Object>();
map1.put('key1','value1111');
map1.put('key2','value2');
map1.put('key3','value3');
Boolean isContainsKey = map1.containsKey('key1');
Map<String,Object> map2 = map1.clone();
Boolean isMapEquals = map1.equals(map2);
Set<String> keySet = map1.keySet();
String value1 = (String)map1.get('key1');
String value2 = (String)map1.get('key2');
String value3 = (String)map1.get('key3');
System.debug('map item:' + value1);
System.debug('map item:' + value2);
System.debug('map item:' + value3);

运行结果:

四)运算及控制语句

运算与控制语句和JAVA基本类似,所以在这里只是简单介绍一下增强for循环

1
2
3
4
5
6
7
List<String> goodsList = new String[] {'衣服','裤子'};
 
for(String goods : goodsList) {
 
    System.debug(goods);
 
}

详细了解这些基本类型变量,集合以及相关方法请自己查看API。其实掌握一些主要的方法就可以,其他方法不用掌握大概清楚实现功能就好,具体需要的时候查看API看一下用法就可以。如果有什么问题,欢迎留言,共同探讨。内容如果有写的不正确地方,欢迎指正。下一篇将主要介绍sObject与简单的DML操作。  

【转载】salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句的更多相关文章

  1. 【转载】salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable

    salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable   本篇知识参考:https://developer.salesforce.com/trailhead/for ...

  2. 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解

    salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解   建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schem ...

  3. 【转载】salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)

    salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)   salesforce中对于数据库操作和JAVA等语言对于数据库操作是有一定区别的.salesfo ...

  4. 【转载】salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载

    salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载   目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新 ...

  5. 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建

    salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建   VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...

  6. salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句

    salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page. 其中Apex语言和java很多的语法类似,今天总结的是一些简单的Apex的变量等知识. 有如下几种常 ...

  7. salesforce 零基础开发入门学习(十五)salesforce中formula的使用(不含Date/Time)

    本文参考官方的formula介绍PDF:https://resources.docs.salesforce.com/200/latest/en-us/sfdc/pdf/salesforce_usefu ...

  8. salesforce 零基础开发入门学习(十二)with sharing 、without sharing 、无声明区别

    在salesforce中,声明类大概可以分成三类:分别是可以声明为with sharing,without sharing,以及两者均不声明. public with sharing class A ...

  9. salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载

    目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新公司,主要做的就是salesforce,不过当时想要看一些相关资料确实比较难.为了避免想要零基础学习的人 ...

随机推荐

  1. 建立django项目的完整流程

    简单的django登录项目 1.首先建立工程,建立工程请参照:https://www.cnblogs.com/effortsing/p/10394511.html 2.在Firstdjango工程项目 ...

  2. Java下载HTTP URL链接示例

    这里以下载迅雷U享版为例. 示例代码: package com.zifeiy.snowflake.handle.filesget; import java.io.File; import java.i ...

  3. 【ARTS】01_37_左耳听风-201900722~201900728

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  4. 01.轮播图之五 :一个 imageView也能 作 轮播

    这个是最近才写的,本以为实现起来很有难度,需要更高深的理论, 写完之后,才发现自己错误的离谱: 之所以能用一个imageview 实现轮播 基于两点::: 使用 imageview 的layer 层设 ...

  5. 字符串匹配算法---BF

    Brute-Force算法,简称BF算法,是一种简单朴素的模式匹配算法,常用语在一个主串string 内查找一个子串 pattern的出现位置. 核心思想: i 遍历主串string i 每自增一次, ...

  6. F2812 DSP程序运行在片内RAM和FLASH的区别

    F2812 DSP程序运行在片内RAM和片内FLASH的区别 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:F2812是带有内部Flash的DSP,与 ...

  7. S12. Android 检查更新功能实现

    [概述] 不需要从 App Store 或者指定官网直接下载,可以通过 App 直接更新到最新版本. [流程设计] 显示当前版本信息以及版本更新日志 提供 “检查更新” 按钮,点击事件处理逻辑: 1) ...

  8. 方法重载,new,override

    方法重载:参数列表不一样,方法名字一样,包括泛型,和返回值无关 new: 复写,方法重载   overload  继承是对于普通方法和属性  复写 父类的 override:覆盖,重写 ,  对于抽象 ...

  9. 服务提供者框架讲解 之 myJDBC

    利用一个简单的myJDBC,讲一下'服务提供者框架'的思想.主要是思想 目录 什么是 服务提供者框架 代码讲解 服务接口 服务提供者接口 服务注册API.服务访问API 静态工厂方法 服务实现类 – ...

  10. C/C++内存知识(一)

    一.C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)- 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. 2.堆区(heap)- 由程序 ...