从string中获取名称variables

示例代码:

int width = 5; int area = 8; int potato = 2; int stackOverflow = -4; 

现在,说我想让用户input一个string:

 String input = new Scanner(System.in).nextLine(); 

然后,说用户inputpotato 。 我将如何检索名为potato的variables,并用它做东西? 像这样的东西:

 System.getVariable(input); //which will be 2 System.getVariable("stackOverflow"); //should be -4 

我抬头看了一些东西,没有多less东西; 我确实find了一个名为“Reflection API”的参考,但是对于这个简单的任务来说这似乎太复杂了。

有没有办法做到这一点,如果是的话,这是什么? 如果“反思”确实有效,如果是唯一的方法,那么我将如何使用它来做到这一点? 它的教程页面有各种内部的东西,我不能有任何意义。

编辑:我需要保持在我正在做的variables的String 。 (我不能使用Map

使用reflection似乎不是一个好的devise,你在这里做什么。 例如Map<String, Integer>使用Map<String, Integer>会更好:

 static final Map<String, Integer> VALUES_BY_NAME; static { final Map<String, Integer> valuesByName = new HashMap<>(); valuesByName.put("width", 5); valuesByName.put("potato", 2); VALUES_BY_NAME = Collections.unmodifiableMap(valuesByName); } 

或用番石榴 :

 static final ImmutableMap<String, Integer> VALUES_BY_NAME = ImmutableMap.of( "width", 5, "potato", 2 ); 

或者与一个枚举 :

 enum NameValuePair { WIDTH("width", 5), POTATO("potato", 2); private final String name; private final int value; private NameValuePair(final String name, final int value) { this.name = name; this.value = value; } public String getName() { return name; } public String getValue() { return value; } static NameValuePair getByName(final String name) { for (final NameValuePair nvp : values()) { if (nvp.getName().equals(name)) { return nvp; } } throw new IllegalArgumentException("Invalid name: " + name); } } 

variables名只在编译时才可用。 reflection只允许访问其中声明的类声明和项目,但不能访问局部variables。 我怀疑某种types的Map对于你真正的问题是一个更合适的解决scheme。 具体来说,检查HashMapTreeMap

为什么不使用带有键/值对的映射,而是试图findvariables名的值?

 Map<String, Integer> vars = new HashMap<String, Integer>(); vars.put("width",5); vars.put("area",8); vars.put("potato", 2); vars.put("stackOverflow",-4); 

那么你可以像这样访问input:

 vars.get(input); //would be 2 vars.get("stackOverflow"); //would be -4 

非常多余 ,但在使用地图时可以保留variables名称:

 int width = 5; int area = 8; int potato = 2; int stackOverflow = -4; Map<String, Integer> map = new HashMap<>(); map.put("width", width); map.put("area", area); map.put("potato", potato); map.put("stackOverflow", stackOverflow); 

但是这样的陈述:

 width = 42; 

不会更改地图中的值:

 String input = "width"; map.get(input); // <-- still returns 5. 

只有一个新的电话修复:

 width = 42; map.put("width", width); // or map.put("width", 42); 

我有一个不涉及使用地图的问题的解决scheme。 我遇到了这种技术,因为我们有几个variables需要根据variables名称本身的内容进行更新。 但是,最好的方法是使用getters / setters而不是variables。

创build完类后,可以通过创buildMethod对象并分别调用它们来访问这些方法。

 public class FooClass private String foo1;<br> private String foo2;<br> public String getFoo1()<br> public String getFoo2()<br> FooClass fooClass = new FooClass(); Method mFoo1 = fooClass.getClass().getMethod("getFoo" + increment + "()"); mFoo1 .invoke(fooClass); However, this would not be limited to only incremental numbers, as long as you can get the string to match the method exactly. String value = "Potato";<br> Method mPotato = myClass.getClass().getMethod("get" + value+ "()"); mPotato.invoke(myClass); 

我有另一个解决scheme没有地图:

 class Vars { Integer potato, stack; public Vars(a,b) { potato=a; stack=b; } } Object object=(Object)new Vars(1,2); Class<?> c = object.getClass(); Integer result=(Integer)c.getField("potato").get(object);