apex 基础语法介绍

字段定义



Integer count = 0;

Decimal total = 18.89;

// 字符串必须使用单引号
String str = 'hello world';

// sObject
Account myAccount = new Account();

所有原始数据类型介绍

  • Blob
  • Boolean
  • Date
  • Datetime
  • Decimal
  • Double
  • ID
  • Integer
  • Long
  • Object
  • String
  • Time

List

有序列表,数组 方法介绍


List<Integer> nums = new List<Integer>();

List<String> strs = new List<String>{'a','b'};

// 嵌套
List<List<List<Integer>>> myLists = new List<List<List<Integer>>>();

// 使用基元或对象的一维列表时,可以使用数组符号来声明和引用
String[] strArr = new List<String>();
List<String> strArr = new String[1];
String[] strArr = new String[1];

// strArr.add()是弹性的,但是strArr[0]就不能数组越界
strArr.add(index, value);
strArr.add(value);
strArr[0] = 'test';

Set

无序列表,元素不可重复 方法介绍

// Set 的语法跟 List 类似
Set<Integer> uniqueNums = new Set<Integer>();

Map

key => value 字典,key为基础类型且不能重复 方法介绍

Map<Integer,String> = new Map<Integer,String>();

Map<String,Integer>  = new Map<String,Integer>{'one' => 1, 'two' => 2};

Enums


public enum Season {SPRING, SUMMER, AUTUMN, WINTER}

Season currentSeason = Season.WINTER;

if (currentSeason == Season.SPRING) {

} else if (currentSeason == Season.WINTER) {

}

Constants

常量

public class myCls {
   static final Integer PRIVATE_INT_CONST = 200;
   static final Integer PRIVATE_INT_CONST2; 

   public static Integer calculate() {
       return 2 + 7;
   }

   static {
       PRIVATE_INT_CONST2 = calculate();
   }
}

判断

Integer a = 666;

if (a == 1) {
    System.debug('if');
} else if (a == 2) {
    System.debug('else if');
} else {
    System.debug('else');
}

switch on a {
    when 2 {
        System.debug('case 1');
    }
    when 666,888 {
        System.debug('case 2');
    }
    when else {
        System.debug('default');
    }
}

循环

Integer count = 0;

do {
    count++;
    System.debug(count);
} while(count < 10);


while(count < 10){
    count++;
    System.debug(count);
}

for (Integer i = 0; i < 10; i++) {
    System.debug(i);
}


Integer[] myInts = new Integer[]{1,2,3,4,5,6,7};

for (Integer i : myInts) {
    System.debug(i);
}

异常

public class MyException extends Exception {}

try {
  throw new MyException('需要继承一下');
} catch(Exception e) {
    System.debug('捕获异常' + e.getMessage());
}

日志

设置 -> 调试日志 -> 新建 -> 按需配置 -> 保存

System.debug("日志内容");

results matching ""

    No results matching ""