如何将枚举与Java中的开关情况使用?
以前,我在sharedPreferencesManager
类中保存了键,
public static final int KEY_COUNTRY_ID = 5,
KEY_COUNTRY_NAME = 10,
KEY_CURRENCY_ID = 15,
KEY_CURRENCY_NAME = 20;
我决定使用枚举存储钥匙,因为我认为这是一种比旧方法更好的方法,这是我尝试的。
sharedPreferencesManager.java
public class SharedPreferencesManager {
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
private static void initializeSharedPreferences(Context context) {
if (sharedPreferences == null)
sharedPreferences = context.getSharedPreferences("SharedPreferencesManager", Context.MODE_PRIVATE);
}
private static void initializeEditor() {
if (editor == null)
editor = sharedPreferences.edit();
}
public static void setValue(Context context, String key, int value) {
initializeSharedPreferences(context);
initializeEditor();
editor.putInt(key, value);
editor.apply();
}
public static int getIntegerValue(Context context, String key) {
initializeSharedPreferences(context);
switch (key) {
case Keys.COUNTRY_ID.name():
case Keys.CURRENCY_ID.name():
return sharedPreferences.getInt(key, 5);
}
return -1;
}
public enum Keys {
COUNTRY_ID,
COUNTRY_NAME,
CURRENCY_ID,
CURRENCY_NAME
}
}
错误
Constant expression required
case> case keys.country_id.name()下的红线():和case keys.currency_id.name():
如何我可以解决问题吗?
Previously, I was saving the keys inside SharedPreferencesManager
class using this way
public static final int KEY_COUNTRY_ID = 5,
KEY_COUNTRY_NAME = 10,
KEY_CURRENCY_ID = 15,
KEY_CURRENCY_NAME = 20;
I decided to use an enum to store the keys because I think it is a better way than the old way, Here is what I tried.
SharedPreferencesManager.java
public class SharedPreferencesManager {
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
private static void initializeSharedPreferences(Context context) {
if (sharedPreferences == null)
sharedPreferences = context.getSharedPreferences("SharedPreferencesManager", Context.MODE_PRIVATE);
}
private static void initializeEditor() {
if (editor == null)
editor = sharedPreferences.edit();
}
public static void setValue(Context context, String key, int value) {
initializeSharedPreferences(context);
initializeEditor();
editor.putInt(key, value);
editor.apply();
}
public static int getIntegerValue(Context context, String key) {
initializeSharedPreferences(context);
switch (key) {
case Keys.COUNTRY_ID.name():
case Keys.CURRENCY_ID.name():
return sharedPreferences.getInt(key, 5);
}
return -1;
}
public enum Keys {
COUNTRY_ID,
COUNTRY_NAME,
CURRENCY_ID,
CURRENCY_NAME
}
}
The error
Constant expression required
The red lines under case Keys.COUNTRY_ID.name(): and case Keys.CURRENCY_ID.name():
How can I solve the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如编译器告诉您的那样,如果您使用
switch(ArtsingVariable)
,则需要使用常数字符串表达式。正确的方法是将String
值转换为枚举常数,然后使用开关。使用此名称将字符串瞬间转换为枚举瞬间
(匹配
name()
)。然后在开关中使用它:
As the compiler tells you, you need to use a constant String expression if you use
switch(aStringVariable)
. The correct way to do this is to convert theString
value to an enum constant and then use switch on that.Convert the String to an enum instant using
This looks up an enum constant by that name (matching the
name()
).Then use it in the switch: