Apex中如何向方法传递枚举类型参数?调用报变量不存在如何解决
问题原因
你定义的Season枚举是EnumTest类的内部枚举,引用时必须携带外层类名作为前缀,否则Apex无法识别Season类型的定义,就会抛出「variable season does not exist」错误。
正确调用方式
// 传参规则:外层类名.枚举名.枚举值 EnumTest.SeasonFinder(EnumTest.Season.Spring); EnumTest.SeasonFinder(EnumTest.Season.Summer); EnumTest.SeasonFinder(EnumTest.Season.fall); EnumTest.SeasonFinder(EnumTest.Season.winter);
额外注意点
- 你的代码存在大小写不一致的隐患:枚举定义中冬季值为小写
winter,但判断分支里写的是Season.Winter(首字母大写),Apex区分大小写,会导致传入冬季参数时无法匹配到对应分支,需要将判断分支的Season.Winter修改为Season.winter与定义保持一致。 - 如果你希望调用时无需携带外层类名,可以将
Season枚举单独定义为顶层枚举(新建独立的枚举类文件),即可直接使用Season.xxx的格式传参。
内容的提问来源于stack exchange,提问作者Richard
相关产品推荐
相关产品推荐

