【问题标题】:java enums - cannot convert from MyClass.Result to intjava enums - 无法从 MyClass.Result 转换为 int
【发布时间】:2014-09-25 16:34:21
【问题描述】:

我在存储枚举值然后通过toString() 方法查询它时遇到问题。我需要对数字进行手工编码。如何安全地覆盖我的toString()

public enum Result {
        SUCCESS(0),
        INVALID_UPLOAD_KEY(-20),
        UPLOAD_KEY_NOT_FOUND(-80);

        private final int value;  //do I need this?

        private Result(int value) {
            this.value = value;
        }

        public static Result fromInt(final int n) {
            for (final Result result : values()) {
                if (result.getValue() == n) {
                    return result;
                }
            }
            return null;
        }


        public int getValue() {
            return this.value;
        }

        public String toString() {
            switch (this.value) {
                case UPLOAD_KEY_NOT_FOUND: //Type mismatch: cannot convert from MyClass.Result to int
                    return "Upload Key not found";
                case INVALID_UPLOAD_KEY: //Type mismatch: cannot convert from MyClass.Result to int
                    return "Invalid Upload Key";
                case SUCCESS: //Type mismatch: cannot convert from MyClass.Result to int
                    return "Success";
                default:
                    return "No result code associated with: " + this.value;
            }
        }
    }

Eclipse 抱怨toString 方法

Type mismatch: cannot convert from MyClass.Result to int

【问题讨论】:

  • 你为什么用泛型标记这个?您认为仿制药会在哪里出现问题?

标签: java enums tostring value-of


【解决方案1】:

其他人已经解释了您的 switch 语句失败的原因 - 除非您出于其他原因需要这些数字,否则您可以摆脱它们。但是您根本不需要 switch 语句 - 只需将 message 设为实例变量:

public enum Result {
    SUCCESS("Success"),
    INVALID_UPLOAD_KEY("Invalid Upload Key"),
    UPLOAD_KEY_NOT_FOUND("Upload Key not found");

    private final String message;

    private Result(String message) {
        this.message = message;
    }

    @Override public String toString() {
        return message;
    }
}

当然,如果您出于其他原因仍需要整数值,也可以保留它。

顺便说一句,我希望覆盖toString(),而是提供getMessage() 方法。

【讨论】:

  • 我能想到重写 toString 方法的一个原因是将枚举添加到组合框,但仅此而已。
【解决方案2】:

这个

switch (this.value) {
case UPLOAD_KEY_NOT_FOUND: 
  return "Upload Key not found";
case INVALID_UPLOAD_KEY:
  return "Invalid Upload Key";
case SUCCESS:
  return "Success";
default:
  return "No result code associated with: " + this.value;
}

应该是

switch (this) {
case UPLOAD_KEY_NOT_FOUND: 
  return "Upload Key not found";
case INVALID_UPLOAD_KEY:
  return "Invalid Upload Key";
case SUCCESS:
  return "Success";
default:
  return "No result code associated with: " + this.value;
}

您在案例陈述中使用了enum 类型。如果需要使用fromInt(int),您仍然需要分配的数值。

【讨论】:

  • 我可以使用 ordinal() 来获取 VM 内部分配的数字吗?
  • 是的,但它们将是连续的,而不是负数。
【解决方案3】:

打开enum 值时,您可以在switch 语句中使用enum 本身。无需查看您自己的自定义值。你可以试试:

switch (this) {
   case UPLOAD_KEY_NOT_FOUND:
       return "Upload Key not found";
   case INVALID_UPLOAD_KEY:
       return "Invalid Upload Key";
   case SUCCESS:
       return "Success";
   default:
       return "No result code associated with: " + this.value;
 }

【讨论】:

  • 对于您的 toString 方法,您拥有的数字 (0, -20, -80) 不是必需的。它们似乎仅在您的 fromInt 方法中是必需的,用于从 int 转换为您的 Result 类型。
  • 我不确定。有没有办法告诉?我正在使用别人的代码。
  • 您需要这些具体数字吗? ordinal() 方法将返回 Java 自己的内部数字,但您无法控制它们是什么数字。
  • 好的,看来我需要这些数字。 fromInt() 在项目的其他地方被引用。
【解决方案4】:

switch 总是需要常量值(一个 int 或常量字符串)。

您可以阅读更多here

在你的情况下,你应该这样做:

switch (this) {
    case -80: 
        return "Upload Key not found";
    case -20:
        return "Invalid Upload Key";
    case 0:
        return "Success";
    default:
        return "No result code associated with: " + this.value;
}

或者不要使用 switch 语句并尝试这样的事情:

return WordUtils.capitilize(this.getName().replace("_"))

WordUtils 是这个library

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2013-02-28
相关资源
最近更新 更多