简介:在将enum和switch case结合使用的过程中,遇到了这个错误:“An enum switch case label must be the unqualified name of an enumeration constant”。 enum和switch case结合使用 在将enum和switch case结合使用的过程中,遇到了这个错误:“An enum switch case label must be the unqualified name of an enume...
因为switch 语句的“标签”需要是编译时常量,并且 an 的属性enum不符合条件。 它们需要成为编译时常量的原因是编译器需要检查开关标签是否不同。它不能允许这样的事情 switch (someValue) { case A.method(): doA(); case B.method(): doB(); } Run Code Online (Sandbox Code Playgroud) 其中A.method(...
但Eclipse继续提示错误信息:The qualified case label TradeStatus.CREATE must be replaced with the unqualified enum constant CREATE。。。 百思不得其解。 问题解决 把枚举常量前的冗余类信息去掉即可,如下所示: Java代码 收藏代码 private String getStatusDesc(Integer tradeStatus) { switch (TradeStatus.get...
switch(expression){casevalue://语句break;//可选casevalue://语句break;//可选//你可以有任意数量的case语句default://可选//语句} 1 2 3 4 5 6 7 8 9 10 11 这里的expression都支持哪些类型呢? 基本数据类型:byte, short, char, int 包装数据类型:Byte, Short, Character, Integer 枚举类型:Enum ...
Enums are strongly typed constants and very useful in programming when your program needs constant values. The following C# program shows how to Enum work with Switch...case
今天在代码中想对Java switch case 和枚举类型Enum对象进行联合使用,但发现有Eclipse中异常提示信息:case expressions must be constant expressions,导致编译始终过不去。 枚举类型定义如下: public enum TradeStatus { CLOSE(-1, "已关闭"), NO_TRADE(0, "未创建"), CREATE(1, "拍下"), PAY(2, "已付款...
Switch Case语句常用来和break一起用,break是可选的。 我们先用一个没有break语句的例子,然后我们再讨论switch case和break。 一个简单的switch case语句 public class SwitchCaseExample1 { public static void main(String args[]){ int num=2; switch(num+2) ...
publicclassSwitchWithEnum{publicstaticvoidmain(String[] args){DayOfWeekday=DayOfWeek.MONDAY; performActions(day); }publicstaticvoidperformActions(DayOfWeek day){switch(day) {caseMONDAY: System.out.println("Monday is the first day of the week.");break;caseTUESDAY: ...
switch case语句的用法 1.switch支持部分基本数据类型(primitive data types),如:byte、short、int、long、char;不支持boolean、float、double。 如图的例子: 2.支持Enum类型、String、和部分基本类型的包装类(如:Character、Byte、Short、Integer); 如图的例子: 3.break关键字可以结束switch语句,如果没有 Java switc...
枚举是一种static final的常量,但是在switch使用时和平时引用静态常量有区别。因为switch条件中的枚举类型必须和case语句里面的一致,所以当在switch语句中声明了枚举类型之后,case语句就已经确定了枚举类型,所以不需要在使用枚举类来引用了。 三、代码示例 public enum Day { ...