0

我在存储枚举值然后通过该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
4

4 回答 4

2

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

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()方法。

于 2014-09-25T16:41:45.870 回答
1

这个

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)

于 2014-09-25T16:38:09.490 回答
0

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

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;
 }
于 2014-09-25T16:36:48.757 回答
0

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

你可以在这里阅读更多

在您的情况下,您应该执行以下操作:

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 是这个的位置。

于 2014-09-25T16:52:56.180 回答