赞
踩
Java switch case 和枚举类型Enum对象进行联合使用,发现Eclipse中异常提示信息:case expressions must be constant expressions,导致编译失败。
枚举类型定义
- public enum FreqCappingType
- {
- MediaBuy(1), Campaign(2),SellStrategy(3),SellCampaign(4);
-
- private int val;
-
- FreqCappingType(int val)
- {
- this.val = val;
- }
-
- public int getVal()
- {
- return val;
- }
- public static FreqCappingType convertFrom(int val)
- {
- switch (val)
- {
- case 1:
- return MediaBuy;
- case 2:
- return Campaign;
- case 3:
- return SellStrategy;
- case 4:
- return SellCampaign;
- }
- return null;
- }
- }
- switch (FreqCappingType.convertFrom(capRecord.getType())) {
- case MediaBuy:
- if (!buyStrategyMap.containsKey(capRecord.getId())) {
- capRecords.remove(capRecord);
- i--;
- continue;
- }
- break;
- case Campaign:
由于 FreqCappingType.MediaBuy.getVal() 返回值是不确定的值,不符合Java switch case的语法必须为常量,导致报错。
红色标记MediaBuy不要写为FreqCappingType.MediaBuy(否则,继续报错The qualified case label FreqCappingType.MediaBuymust be replaced with the unqualified enum constant MediaBuy)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。