在Java中按名称设置变量

我希望用Java来实现一些东西:

class Foo{ private int lorem; // private int ipsum; public setAttribute(String attr, int val){ //sets attribute based on name } public static void main(String [] args){ Foo f = new Foo(); f.setAttribute("lorem",1); f.setAttribute("ipsum",2); } public Foo(){} } 

…基于变量名设置变量而不使用硬编码的变量名并且不使用任何其他数据结构。 这可能吗?

以下是使用reflection实现setAttribute方法(我已重命名该函数;不同的字段类型有不同的reflection函数):

 public void setIntField(String fieldName, int value) throws NoSuchFieldException, IllegalAccessException { Field field = getClass().getDeclaredField(fieldName); field.setInt(this, value); } 

通常,您希望使用Reflection。 以下是对主题的一个很好的介绍和示例

特别是,“更改字段值”部分描述了如何执行您想要执行的操作。

我注意到作者说,“这个function非常强大,与其他传统语言没有相同之处。” 当然,在过去的十年中(文章写于1998年),我们看到了动态语言的巨大进步。 上面的内容很容易用Perl,Python,PHP,Ruby等完成。 我怀疑这是你可能来自“eval”标签的方向。

另外,看看BeanUtils ,它可以隐藏使用reflection的一些复杂性。

根据用途,您可以使用上面建议的reflection,或者HashMap可能更适合……

问题是针对整数的,这是有帮助的,但是这里有一些更普遍的东西。 如果要加载字段名称/字段值对的String表示forms,则此类方法很有用。

 import java.lang.reflect.Field; public class FieldTest { static boolean isValid = false; static int count = 5; public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { FieldTest test = new FieldTest(); test.setProperty("count", "24"); System.out.println(count); test.setProperty("isValid", "true"); System.out.println(isValid); } public void setProperty(String fieldName, String value) throws NoSuchFieldException, IllegalAccessException { Field field = this.getClass().getDeclaredField(fieldName); if (field.getType() == Character.TYPE) {field.set(getClass(), value.charAt(0)); return;} if (field.getType() == Short.TYPE) {field.set(getClass(), Short.parseShort(value)); return;} if (field.getType() == Integer.TYPE) {field.set(getClass(), Integer.parseInt(value)); return;} if (field.getType() == Long.TYPE) {field.set(getClass(), Long.parseLong(value)); return;} if (field.getType() == Float.TYPE) {field.set(getClass(), Float.parseFloat(value)); return;} if (field.getType() == Double.TYPE) {field.set(getClass(), Double.parseDouble(value)); return;} if (field.getType() == Byte.TYPE) {field.set(getClass(), Byte.parseByte(value)); return;} if (field.getType() == Boolean.TYPE) {field.set(getClass(), Boolean.parseBoolean(value)); return;} field.set(getClass(), value); } } 

您可能希望在使用时缓存一些reflection数据:

 import java.lang.reflect.Field; import java.util.HashMap; class Foo { private HashMap fields = new HashMap(); private void setAttribute(Field field, Object value) { field.set(this, value); } public void setAttribute(String fieldName, Object value) { if (!fields.containsKey(fieldName)) { fields.put(fieldName, value); } setAttribute(fields.get(fieldName), value); } }