Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从字符串变量获取自定义对象属性值_Java_Android_Class - Fatal编程技术网

Java 从字符串变量获取自定义对象属性值

Java 从字符串变量获取自定义对象属性值,java,android,class,Java,Android,Class,我知道在JavaScript中,您可以调用myCustomObject['something']并获取值 在Java中有这样做的方法吗? 我有一个自定义对象学生: public class Student { private String name; private String id; private String year; private String homeroom; public String getName() { return na

我知道在JavaScript中,您可以调用
myCustomObject['something']
并获取值

在Java中有这样做的方法吗?

我有一个自定义对象学生

public class Student  {

    private String name;
    private String id;
    private String year;
    private String homeroom;

    public String getName() {  return name; }

    public void setName(String name) { this.name = name; }

    public String getId() {  return id; }

    public void setId(String id) { this.id = id; }

    public String getYear() { return year; }

    public void setYear(String year) { this.year = year; }

    public String getHomeroom() { return homeroom;  }

    public void setHomeroom(String homeroom) { this.homeroom = homeroom;  }

}
我想要一种调用方法来获取属性的方法,例如
Student.getValueForKey(key)
。其中,
key
可以是学生类的任何属性:名称、id、年份等

我不想调用实际的getter:
getId()
getYear()
等等


我尝试了
student[id]
,但对象不是数组。我需要一种通过变量获取属性的方法。

你不能,在java中调用getId()是fundametal,另一种方法是通过反射调用它,但它会影响你的性能,而且需要更多的代码来设置

你不能,在java中调用getId()是fundametal,另一种方法是通过反射调用它,但是它会影响您的性能,并且需要更多的代码来设置

1)您可以使用反射来检索与字符串名称参数匹配的字段名称并获取相关值

2) 如果合适的话,一个更优雅的解决方案可以是用
Enum
替换Student类来存储Student的属性,并用
EnumMap
实例来表示个人:

public enum StudentAttributes {
    NAME, ID, YEAR, HOME_ROOM;
}
然后做:

EnumMap<StudentAttributes, String> studentOne = new EnumMap<StudentAttributes, String>(StudentAttributes.class);

studentMap.put(StudentAttributes.NAME, "john");
studentMap.put(StudentAttributes.YEAR, "1945");
...
String name = studentMap.get(StudentAttributes.NAME);
String year = studentMap.get(StudentAttributes.YEAR);
EnumMap studentOne=新的EnumMap(StudentAttributes.class);
studentMap.put(StudentAttributes.NAME,“john”);
学生地图(studentMap.put)(StudentAttributes.YEAR,1945年);
...
String name=studentMap.get(StudentAttributes.name);
字符串year=studentMap.get(StudentAttributes.year);
1)您可以使用反射来检索与字符串名称参数匹配的字段名,并获取相关值

2) 如果合适的话,一个更优雅的解决方案可以是用
Enum
替换Student类来存储Student的属性,并用
EnumMap
实例来表示个人:

public enum StudentAttributes {
    NAME, ID, YEAR, HOME_ROOM;
}
然后做:

EnumMap<StudentAttributes, String> studentOne = new EnumMap<StudentAttributes, String>(StudentAttributes.class);

studentMap.put(StudentAttributes.NAME, "john");
studentMap.put(StudentAttributes.YEAR, "1945");
...
String name = studentMap.get(StudentAttributes.NAME);
String year = studentMap.get(StudentAttributes.YEAR);
EnumMap studentOne=新的EnumMap(StudentAttributes.class);
studentMap.put(StudentAttributes.NAME,“john”);
学生地图(studentMap.put)(StudentAttributes.YEAR,1945年);
...
String name=studentMap.get(StudentAttributes.name);
字符串year=studentMap.get(StudentAttributes.year);