Constant expression required问题解决

在上一篇文章中,我用自己写的枚举类来作Case判断的时候发现了问题。系统一直提示Constant expression required,这是为什么呢?

原因是这样的:switch…case语句的case后面的值必须为常量,因为switch在编译的时候需要确保case里面的值必须不能相同。直接写getXXX这样的方法是可能出现重复的,因为你可以写两个相同的枚举值,但是实际业务场景下,一般也不会有人这样子做。即使如此,编译器还是不认。那怎么办呢,我们加一个方法提前判断就行了。

枚举类

public enum NumberForCaseEnum {
    
    ONE(1),
    TWO(2),
    THREE(3),
    FOUR(4);
    private int index;

    private NumberForCaseEnum(int index){
        this.index=index;
    }
    public int getIndex(){
        return this.index;
    }

    /**
     * 提前判断,用于解决
     * Case中出现的Constant expression required
     * @param value
     * @return
     */
    public static NumberForCaseEnum getByValue(int value){
        for(NumberForCaseEnum x:values()){
            if(x.getIndex()==value){
                return x;
            }
        }
        return null;
    }
}

测试代码:

		//可以通过循环遍历拿值
		for(NumberForCaseEnum x:NumberForCaseEnum.values()){
			System.out.println("枚举值:"+x.getIndex());
		}
		//也可以单独拿值
		System.out.println("调皮捣蛋的:"+ONE.getIndex());
		//
		switch(NumberForCaseEnum.getByValue(2)){
			case ONE:
				System.out.println("1111");
				break;
			case TWO:
				System.out.println("精明可爱的:2222");
				break;
			case THREE:
				System.out.println("3333");
				break;
			default:
		}

运行结果:
在这里插入图片描述
关注公众号,获取更多资源
在这里插入图片描述

每天进步一点点,开心也多一点点

  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
****************************** * 编 译 错 误 信 息 * ******************************* '''';'''' not allowed before ''''ELSE'''' ElSE前不允许有“;” '''''''' clause not allowed in OLE automation section 在OLE自动区段不允许“”子句 '''''''' is not a type identifier 不是类型标识符 '''''''' not previously declared as a PROPERTY 前面没有说明PROPERTY ''''GOTO '''' leads into or out of TRY statement GOTO 进入或超出TRY语句的范围 clause expected, but found 要求子句,但出现 16-Bit fixup encountered in object file '''''''' 在对象文件遇到16位修复 486/487 instructions not enabled 不能用486/487指令 Abstract methods must be virtual or dynamic 抽象方法必须为虚拟的或动态的 Array type required 需要数组类型 Assignment to FOR-Loop variable '''''''' 给FOR循环变量赋值 Bad argument type in variable type array constructor 在变量类型数组结构中不正确的参数类型 Bad file format '''''''' 错误的文件格式 Bad file format: 错误的文件格式 Bad global symbol definition: '''''''' in object file '''''''' 对象文件''''''''中错误的全局符号定义'''''''' Bad unit format: 错误的单元格式 BREAK or CONTINUE outside of loop BREAK或CONTINUE超出循环 Cannot add or subtract relocatable symbols 不能增加或减少可重置的符号 Cannot assign to a read-only property 不能指定只读属性 Cannot BREAK, CONTINUE or EXIT out of a FINALLY clause 超出FINALLY子句的范围,不能使用BREAK,CONTINUE或EXIT语句 Cannot initialize local variables 不能初始化局部变量 Cannot initialize multiple variables 不能初始化多个变量 Cannot initialize thread local variables 不能初始化线程局部变量 Cannot override a static method 不能覆盖静态方法 Cannot read a write-only property 不能读取只写属性 Case label outside of range of case expression CASE标号超出了CASE表达式的范围 Circular unit reference to 对单元循环引用 Class already has a default property 类已具有默认的属性 Class does not have a default property 类没有默认的属性 Class or object types only allowed in type section 在类型区段只允许有类或对象类型 Class type required 需要类类型 Close error on 文件关闭错误 Compile terminated by user 用户中止编译 Constant expected 要求常量 Constant expression expected 要求常量表达式 Constant expression violates subrange bounds 常量表达式超出子界范围 Constant object cannot be passed as var parameter 常量对象不能作为变量参数传递 Constant or type identifier expected 要求常量或类型标识符 Constants cannot be used as open array arguments 常量不能用作打开数组参数 Constructing instance of '''''''' containing abstract methods 构造的实体包含抽象的方法 Could not compile used unit '''''''' 不能用单元编译 Could not create output file 不能建立输出文件 Could not load RLINK32.DLL 不能加载RLINK32.DLL Data type too large: exceeds 2 GB 数据类型太大:超过2GB Declaration of differs from previous declaration 的说明与先前的说明不同 Default property must be an array property 默认的属性必须为数组属性 Default values must be of ordinal, pointer or small set type 默认的值必须为序数、指针或小集类型 Destination cannot be assigned to 目标不能指定 Destination is inaccessible 目标不能存取 Dispid '''''''' already used by '''''''' DISPID标识号已被使用 Dispid clause only allowed in OLE automation section DISPID子句只能在OLE自动区段中使用 Division by zero 除数为零 Duplicate case label CASE标号重复 Duplicate tag value 重复的标志值 Dynamic method or message handler not allowed here 这里不允许有动态方法或信息处理程序 Dynamic methods and message handlers not allowed in OLE automation section在OLE自动区段不允许有动态方法或消息处理程序 Element 0 inaccessible - use ''''Length'''' or ''''SetLength'''' 元素0不能存取-使用LENGTH或SETLENGTH Error in numeric constant 数值常量错误 EXCEPT or FINALLY expected 要求EXCEPT或FINALLY EXPORTS allowed only at global scope EXPORTS只允许在全局范围使用 Expression has no value 表达式没有值 Expression too complicated 表达式太复杂 Field definition not allowed in OLE automation section 在OLE自动区段中不允许域定义 Field definition not allowed after methods or properties 在方法或属性后不允许域定义 Field or method identifier expected 要求域或方法标识符 File not found: 文件没有找到 File type not allowed here 这儿不允许文件类型 For loop control variable must be simple local variable FOR循环控制变量必须为简单局部变量 For loop control variable must have ordinal type FOR循环控制变量必须为序数类型 FOR or WHILE loop executes zero times - deleted FOR或WHILE循环执行零次-删除 FOR-Loop variable '''''''' cannot be passed as var parameter FOR循环变量不能作为参数传递 FOR-Loop variable '''''''' may be undefined after loop 在循环后的FOR循环变量是不确定的 Function needs result type 函数需要结果类型 Identifier redeclared: '''''''' 标识符重复说明 Illegal character in input file: '''''''' ($) 在输入文件中的非法字符'''''''' Illegal message method index 非法的消息方法指针 Illegal reference to symbol '''''''' in object file '''''''' 在对象文件中对符号的非法引用 Illegal type in OLE automation section: '''''''' 在OLE自动区段中的非法类型 Illegal type in Read/Readln statement 在Read/Readln语句中的非法类型 Illegal type in Write/Writeln statement 在Write/Writeln语句中的非法类型 Inaccessible value 不可存取的值 Incompatible types: '''''''' and '''''''' 不兼容的类型和 Incompatible types: 不兼容的类型 Inline assembler stack overflow 内联汇编溢出 Inline assembler syntax error 内联汇编语法错误 Instance variable '''''''' inaccessible here 实体变量在这里不能存取
下面是整合的的步骤 spring整合hibernate 加入jar包 加入spring和aop所需必须包 加入hibernate的必须包 spring整合hibernate的必须包 org.springframework.jdbc-3.1.3.RELEASE.jar org.springframework.orm-3.1.3.RELEASE.jar org.springframework.transaction-3.1.3.RELEASE.jar 加入配置文件 加入spring的配置文件 加入hibernate的配置文件 加入配置代码 加入对SessionFactory的配置 加入数据源(DataSource)的配置 <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value=""/> </bean> 加入SessionFactory的配置 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置session factory使用的数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 配置使用hibernate的配置文件 --> <!--<property name="configLocation" value="classpath*:hibernate.cfg.xml" /> --> <!-- 无需写hibernate的配置文件,而是将hibernate的配置直接加入到spring配置文件中 配置hibernate的映射文件地址 --> <property name="mappingResources"> <list> <value>com/direct/domain/Employee.hbm.xml</value> <value>com/direct/domain/Log.hbm.xml</value> </list> </property> <!-- 配置hibernate的其他属性 --> <property name="hibernateProperties"> <map> <entry key="dialect" value="org.hibernate.dialect.MySQL5Dialect" /> <entry key="show_sql" value="true" /> <entry key="format_sql" value="true" /> <entry key="current_session_context_class" value="thread" /> </map> </property> </bean> 加入事务管理切面类的配置 <!-- 创建事务管理器(spring针对hibernate实现的事务管理的切面类) --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事务的通知类型 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="*" read-only="true" /> <!-- 或者 <tx:method name="*list*" read-only="true"/> <tx:method name="*get*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> --> </tx:attributes> </tx:advice> 将切面类应用到切入点上 <!-- 将事务管理规则的切面应用到对应的切入点 --> <aop:config> <aop:pointcut expression="execution(* com.direct.service.*.*(..))" id="transactionPointCut"/> <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointCut"/> </aop:config> spring和struts2的整合 加入整合包 加入struts的必须包 struts整合spring的包 struts2-spring-plugin-2.3.15.3.jar spring整合struts的包 org.springframework.web-3.1.3.RELEASE.jar org.springframework.web.servlet-3.1.3.RELEASE.jar org.springframework.web.struts-3.1.3.RELEASE.jar 加入struts的配置文件struts.xml 在web.xml中配置struts <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 在web.xml配置spring <!-- 配置spring的配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <!-- 配置spring随web容器启动时就创建 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 在struts.xml中配置对象创建工具为spring <constant name="struts.objectFactory" value="spring" />

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

white_poland

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值