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