'equals' 比较字符串和枚举类型: String 和 ExportTypeEnum
要解决'equals'比较字符串和枚举类型(String 和 ExportTypeEnum)时遇到的错误,您需要将它们进行比较,但它们是不可转换的类型。您可以尝试以下几种方法来解决这个问题:
-
使用字符串表示的枚举值进行比较:
String exportType = 'EXPORT_TYPE_ONE'; if (exportType.equals(ExportTypeEnum.EXPORT_TYPE_ONE.toString())) { // 执行相应的操作 } -
将字符串转换为枚举类型进行比较:
String exportType = 'EXPORT_TYPE_ONE'; ExportTypeEnum exportTypeEnum = ExportTypeEnum.valueOf(exportType); if (exportTypeEnum == ExportTypeEnum.EXPORT_TYPE_ONE) { // 执行相应的操作 } -
在 ExportTypeEnum 中添加一个静态方法,用于将字符串转换为对应的枚举值:
public enum ExportTypeEnum { EXPORT_TYPE_ONE, EXPORT_TYPE_TWO, EXPORT_TYPE_THREE; public static ExportTypeEnum fromString(String value) { for (ExportTypeEnum exportType : ExportTypeEnum.values()) { if (exportType.toString().equalsIgnoreCase(value)) { return exportType; } } throw new IllegalArgumentException('Invalid ExportTypeEnum value: ' + value); } }然后使用该方法进行比较:
String exportType = 'EXPORT_TYPE_ONE'; if (ExportTypeEnum.fromString(exportType) == ExportTypeEnum.EXPORT_TYPE_ONE) { // 执行相应的操作 }
请根据您的具体需求选择适合的方法。
原文地址: https://www.cveoy.top/t/topic/qs6y 著作权归作者所有。请勿转载和采集!