dynamicvariables名称Java
我将如何能够retreive具有dynamic名称的variables的值
例如,我有常量列表
public class Constant{ public static final String S_R = "Standard(240)"; public static final String S_W = "Standard(180)"; public static final String L_R = "Large(360)"; public static final String L_W = "Large(280)"; } 基于数据库我build立一个variables名称
 String varName = "S" + "_" +"R"; // This can be S_R , S_W , L_R or L_W String varVal = // How do i get value of S_R 
	
使用具有variables名称的普通HashMap作为string的值。 或者使用枚举作为键和值作为值的EnumMap。 AFAIK,这是使用Java时最接近的。 当然,你可以用reflection来搞乱,但IMO的地图方法更合乎逻辑。
 您可以使用Map<String, String>并通过其键find值。 
 更好的是,你可以有一个enum : 
 public enum Foo { S_R("Standard", 240), S_W("Standard", 180),...; private String type; private String duration; // constructor and getters } 
 然后调用Foo.valueOf(name) 
  (你也可以通过reflection来实现 – Constants.class.getField(fieldName)然后调用field.get(null) (静态为field.get(null) ,但这不是一个好方法。 
如果你真的必须这样做(而且不太可能),你将不得不使用Java“reflection”API。