所以这不是“我想在运行时更改一个常量值,为什么它不起作用?”之一。问题,就像提醒一样。我无法在代码中更改 Java Enum 的值。我已经进行了更改、保存并重新运行了我的程序,但代码中的值仍然是旧值。我正在使用 Enum.values() 方法循环遍历我的枚举常量,并且该值显示为旧值,而不是我直接输入枚举的新值。我开始怀疑我是不是疯了,或者我的程序决定不重新编译我的枚举或什么。欢迎进行健全性检查。有关我的枚举代码的全部内容以及我在何处访问这些值,请参见下文。
public enum MSSQLType implements DBType {
//String Types
CHAR(8000, -1, null, "CHAR","TEXT"),
VARCHAR(8000, -1, null, "VARCHAR", "TEXT"),
TEXT(-1, -1, null, "TEXT", "TEXT"),
NCHAR(4000, -1, null, "CHAR", "TEXT"),
NVARCHAR(4000, -1, null, "VARCHAR", "TEXT"),
NTEXT(-1, -1, null, "TEXT", "TEXT"),
//Numeric Types
BIGINT(19, -1, null, "BIGINT", "BIGINT"),
BIGINT_IDENTITY (19, -1, null, null, null),
INT(10, -1, null, "INT", "BIGINT"),
SMALLINT(5, -1, null, "SMALLINT", "BIGINT"),
TINYINT(3, -1, null, "TINYINT", "BIGINT"),
BIT(1, -1, null, "TINYINT", "BIGINT"),
DECIMAL(14, 38, null, "DECIMAL", "DECIMAL"),
NUMERIC(14, 38, null, "DECIMAL", "DECIMAL"),
MONEY(15, 4, null, "DECIMAL", "DECIMAL"),
SMALLMONEY(6,4, null, "DECIMAL", "DECIMAL"),
FLOAT(-1, 53, null, "FLOAT", "FLOAT"),
REAL(-1, 106, null, "FLOAT", "FLOAT"),
//Date/time types
DATETIME(-1, -1, "YYYY-mm-DD HH:MM:SS", "DATETIME", "DATETIME"),
SMALLDATETIME(-1, -1, "YYYY-mm-DD HH:MM:SS", "DATETIME", "DATETIME"),
//Other types
BINARY(-1,-1, null, null, null),
VARBINARY(-1,-1, null, null, null),
IMAGE(-1,-1, null, null, null),
CURSOR(-1,-1, null, null, null),
SQL_VARIANT(-1,-1, null, null, null),
TABLE(-1,-1, null, null, null),
TIMESTAMP(-1, -1, "YYYY-mm-DD HH:MM:SS", "DATETIME", "DATETIME"),
UNIQUEIDENTIFIER(-1, -1, null, null, null);
private int size;
private int precision;
private String format;
private String convertTo;
private String fallback;
MSSQLType(int size, int precision, String format, String convertTo, String fallback) {
this.size = size;
this.precision = precision;
this.format = format;
this.convertTo = convertTo;
this.fallback = fallback;
}
public int getSize() {
return size;
}
public int getPrecision() {
return precision;
}
public String getFormat() {
return format;
}
public String getConvertTo() {
return convertTo;
}
public String getFallback() {
return fallback;
}
@Override
public String getDisplay() {
return toString();
}
@Override
public String toString() {
return super.toString().replaceAll("_", " ");
}
}
因此 REAL 的 convertTo 和 fallback 值从“DOUBLE”、“DOUBLE”更改为“FLOAT”、“FLOAT”,但代码仍然将常量字符串“DOUBLE”分配给这两个值!这些值是这样读出的:
for (MSSQLType msSqlType : MSSQLType.values()) {
if (typeName.equalsIgnoreCase(msSqlType.toString())) {
this.type = msSqlType;
return;
}
}
我也尝试过使用 MSSQLType.class.getEnumConstants() 得到相同的结果。
任何帮助表示赞赏。