final static int
DRAGON = 5,
SNAKE = 6;
String this_item = "DRAGON";
int item_value = super_func(this_item);//must be 5
Is it possible to implement this super_func
, as shown above?
是否可以实现此super_func,如上所示?
Or maybe I am asking for too much?
或许我要求的太多了?
EDIT:
I am not allowed to use enum. I can:
我不被允许使用枚举。我可以:
a) use a map (as someone pointed, or 2 array lists) or
b) do this with some pro way (but it doesn't seem possible).
a)使用地图(如有人指出,或2个数组列表)或b)用一些专业方式(但似乎不可能)这样做。
5 个解决方案
#1
1
While it's technically possible to do this using reflection, I'd strongly recommend you to rethink your structure, and consider one of the alternatives that have been mentioned in the other answers.
虽然技术上可以使用反射来做到这一点,但我强烈建议您重新考虑您的结构,并考虑其他答案中提到的替代方案之一。
The simplest would probably be to use a map
最简单的可能是使用地图
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
public class VariableNameTest
{
private static final Map<String,Integer> map;
static
{
Map<String, Integer> m = new LinkedHashMap<String, Integer>();
m.put("DRAGON",5);
m.put("SNAKE",6);
map = Collections.unmodifiableMap(m);
}
public static void main(String[] args)
{
System.out.println(getValue("DRAGON"));
System.out.println(getValue("SNAKE"));
System.out.println(getValue("Boo!"));
}
public static int getValue(String name)
{
Integer i = map.get(name);
if (i == null)
{
// Do some error handling here!
}
return i;
}
}
But if your intention is to extend this with additional functionality, you should probably introduce a class like it was recommended in https://*.com/a/23846279/3182664
但是如果您打算使用其他功能来扩展它,那么您应该引入类似于https://*.com/a/23846279/3182664中建议的类。
Just for completeness: The reflection solution, not recommended!
只是为了完整性:反射解决方案,不推荐!
import java.lang.reflect.Field;
public class VariableNameTest
{
static final int DRAGON = 5;
static final int SNAKE = 6;
public static void main(String[] args)
{
System.out.println(getValue("DRAGON"));
System.out.println(getValue("SNAKE"));
System.out.println(getValue("Boo!"));
}
private static int getValue(String name)
{
try
{
Class<?> c = VariableNameTest.class;
Field f = c.getDeclaredField(name);
return f.getInt(null);
}
catch (NoSuchFieldException e)
{
// Many things
e.printStackTrace();
}
catch (SecurityException e)
{
// may cause
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
// this to
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// fail horribly
e.printStackTrace();
}
return 0;
}
}
#2
3
I don't know what this value is supposed to represent, but you could use an Enum.
我不知道这个值应该代表什么,但你可以使用枚举。
enum Animal {
DRAGON(5),
SNAKE(6);
private final int a;
private Animal(int a){
this.a = a;
}
public int getA(){
return a;
}
}
And then
String this_item = "DRAGON";
int item_value = Animal.valueOf(this_item).getA();//5
#3
2
You can get the value of a variable with Java Reflection tricks, but why not declaring a simple enum and using it:
您可以使用Java Reflection技巧获取变量的值,但为什么不声明一个简单的枚举并使用它:
enum Creature{
DRAGON(5), SNAKE(6);
...
}
#4
1
As far as I know such a thing is not possible, but you coudl store your variables in Map
据我所知,这样的事情是不可能的,但是你可以将你的变量存储在Map中
HashMap<String,Integer> map = new HashMap<>();
map.put("DRAGON",5);
map.put("SNAKE",6);
int itemValue = map.get("DRAGON"); //Sets itemValue to 5
#5
1
You can also use a switch() statement.
您还可以使用switch()语句。
int item_value;
switch(this_item) {
case "DRAGON":
item_value = 5;
break;
case "SNAKE":
item_value = 6
break;
or different solution doing the opposite
或者不同的解决方案正好相反
private String getAnimal(int itemValue) {
switch(item_value) {
case 5:
return "DRAGON";
case 6:
return "SNAKE";
default:
return null;
}
#1
1
While it's technically possible to do this using reflection, I'd strongly recommend you to rethink your structure, and consider one of the alternatives that have been mentioned in the other answers.
虽然技术上可以使用反射来做到这一点,但我强烈建议您重新考虑您的结构,并考虑其他答案中提到的替代方案之一。
The simplest would probably be to use a map
最简单的可能是使用地图
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
public class VariableNameTest
{
private static final Map<String,Integer> map;
static
{
Map<String, Integer> m = new LinkedHashMap<String, Integer>();
m.put("DRAGON",5);
m.put("SNAKE",6);
map = Collections.unmodifiableMap(m);
}
public static void main(String[] args)
{
System.out.println(getValue("DRAGON"));
System.out.println(getValue("SNAKE"));
System.out.println(getValue("Boo!"));
}
public static int getValue(String name)
{
Integer i = map.get(name);
if (i == null)
{
// Do some error handling here!
}
return i;
}
}
But if your intention is to extend this with additional functionality, you should probably introduce a class like it was recommended in https://*.com/a/23846279/3182664
但是如果您打算使用其他功能来扩展它,那么您应该引入类似于https://*.com/a/23846279/3182664中建议的类。
Just for completeness: The reflection solution, not recommended!
只是为了完整性:反射解决方案,不推荐!
import java.lang.reflect.Field;
public class VariableNameTest
{
static final int DRAGON = 5;
static final int SNAKE = 6;
public static void main(String[] args)
{
System.out.println(getValue("DRAGON"));
System.out.println(getValue("SNAKE"));
System.out.println(getValue("Boo!"));
}
private static int getValue(String name)
{
try
{
Class<?> c = VariableNameTest.class;
Field f = c.getDeclaredField(name);
return f.getInt(null);
}
catch (NoSuchFieldException e)
{
// Many things
e.printStackTrace();
}
catch (SecurityException e)
{
// may cause
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
// this to
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// fail horribly
e.printStackTrace();
}
return 0;
}
}
#2
3
I don't know what this value is supposed to represent, but you could use an Enum.
我不知道这个值应该代表什么,但你可以使用枚举。
enum Animal {
DRAGON(5),
SNAKE(6);
private final int a;
private Animal(int a){
this.a = a;
}
public int getA(){
return a;
}
}
And then
String this_item = "DRAGON";
int item_value = Animal.valueOf(this_item).getA();//5
#3
2
You can get the value of a variable with Java Reflection tricks, but why not declaring a simple enum and using it:
您可以使用Java Reflection技巧获取变量的值,但为什么不声明一个简单的枚举并使用它:
enum Creature{
DRAGON(5), SNAKE(6);
...
}
#4
1
As far as I know such a thing is not possible, but you coudl store your variables in Map
据我所知,这样的事情是不可能的,但是你可以将你的变量存储在Map中
HashMap<String,Integer> map = new HashMap<>();
map.put("DRAGON",5);
map.put("SNAKE",6);
int itemValue = map.get("DRAGON"); //Sets itemValue to 5
#5
1
You can also use a switch() statement.
您还可以使用switch()语句。
int item_value;
switch(this_item) {
case "DRAGON":
item_value = 5;
break;
case "SNAKE":
item_value = 6
break;
or different solution doing the opposite
或者不同的解决方案正好相反
private String getAnimal(int itemValue) {
switch(item_value) {
case 5:
return "DRAGON";
case 6:
return "SNAKE";
default:
return null;
}