Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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_Class - Fatal编程技术网

Java 将值设置为名称与字符串数据匹配的类变量

Java 将值设置为名称与字符串数据匹配的类变量,java,class,Java,Class,场景 我有一个字符串列表,它类似于类的类变量的名称。所以现在我必须使用列表为这些变量设置值 示例 class Abc { private String name; private String Id; private String sal; } public static void main(String args[]){ Map<String,String> variablesMap = new HashMap<String,String>(); variabl

场景

我有一个字符串列表,它类似于类的类变量的名称。所以现在我必须使用列表为这些变量设置值

示例

class Abc {
 private String name;
 private String Id;
 private String sal;
}

public static void main(String args[]){
Map<String,String> variablesMap = new HashMap<String,String>();
variablesMap.add("name","Joshi");
variablesMap.add("id","101");

/*Here i want to write the code to set the values for those variables*/
}
Abc类{
私有字符串名称;
私有字符串Id;
私有字符串sal;
}
公共静态void main(字符串参数[]){
Map variablesMap=newhashmap();
变量映射添加(“名称”、“Joshi”);
变量映射添加(“id”、“101”);
/*在这里,我想编写代码来设置这些变量的值*/
}
尝试过

ABC
包含所有getter和setter。
使用
java.lang.Field
class,我可以通过
ABC.class.getDeclaredFileds()
获得变量列表。但是在那之后,我如何设置这些值。

您可以通过
Field Field=getClass().getDeclaredField(name)为您的名字获取一个
字段

然后看看Java文档: 您可以使用多个选项根据数据类型设置变量的值

看看这个例子:编辑:根据您的确切问题进行更新

import java.lang.reflect.Field;
import java.util.Map;
import java.util.HashMap;

public class FieldTest {

    String name;
    String id;


    public static void main(String[] args) {
        new FieldTest();
    }

    public FieldTest() {
    Map<String,String> variablesMap = new HashMap<String,String>();
    variablesMap.put("name","Joshi");
    variablesMap.put("id","101");

    for (Map.Entry<String, String> entry : variablesMap.entrySet())
    {
        try {
        test(entry.getKey(),entry.getValue());
        }
        catch(NoSuchFieldException ex) {
            ex.printStackTrace();
        }
        catch(IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    System.out.println(name);
    System.out.println(id);
    }

    private void test(String name, String value) throws NoSuchFieldException, IllegalAccessException {
        Field field = getClass().getDeclaredField(name);
        field.set(this,value);
    }

}
import java.lang.reflect.Field;
导入java.util.Map;
导入java.util.HashMap;
公开课实地测试{
字符串名;
字符串id;
公共静态void main(字符串[]args){
新现场测试();
}
公共现场测试(){
Map variablesMap=newhashmap();
可变映射put(“名称”、“Joshi”);
可变映射放置(“id”,“101”);
对于(Map.Entry:variablesMap.entrySet())
{
试一试{
测试(entry.getKey(),entry.getValue());
}
捕获(NoSuchFieldException-ex){
例如printStackTrace();
}
捕获(非法访问例外e){
e、 printStackTrace();
}
}
System.out.println(名称);
系统输出打印项次(id);
}
私有void测试(字符串名称、字符串值)抛出NoSuchFieldException、IllegalAccessException{
Field=getClass().getDeclaredField(名称);
字段.set(此字段,值);
}
}

您可以通过
字段Field=getClass().getDeclaredField(name)为您的名字获取一个
字段

然后看看Java文档: 您可以使用多个选项根据数据类型设置变量的值

看看这个例子:编辑:根据您的确切问题进行更新

import java.lang.reflect.Field;
import java.util.Map;
import java.util.HashMap;

public class FieldTest {

    String name;
    String id;


    public static void main(String[] args) {
        new FieldTest();
    }

    public FieldTest() {
    Map<String,String> variablesMap = new HashMap<String,String>();
    variablesMap.put("name","Joshi");
    variablesMap.put("id","101");

    for (Map.Entry<String, String> entry : variablesMap.entrySet())
    {
        try {
        test(entry.getKey(),entry.getValue());
        }
        catch(NoSuchFieldException ex) {
            ex.printStackTrace();
        }
        catch(IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    System.out.println(name);
    System.out.println(id);
    }

    private void test(String name, String value) throws NoSuchFieldException, IllegalAccessException {
        Field field = getClass().getDeclaredField(name);
        field.set(this,value);
    }

}
import java.lang.reflect.Field;
导入java.util.Map;
导入java.util.HashMap;
公开课实地测试{
字符串名;
字符串id;
公共静态void main(字符串[]args){
新现场测试();
}
公共现场测试(){
Map variablesMap=newhashmap();
可变映射put(“名称”、“Joshi”);
可变映射放置(“id”,“101”);
对于(Map.Entry:variablesMap.entrySet())
{
试一试{
测试(entry.getKey(),entry.getValue());
}
捕获(NoSuchFieldException-ex){
例如printStackTrace();
}
捕获(非法访问例外e){
e、 printStackTrace();
}
}
System.out.println(名称);
系统输出打印项次(id);
}
私有void测试(字符串名称、字符串值)抛出NoSuchFieldException、IllegalAccessException{
Field=getClass().getDeclaredField(名称);
字段.set(此字段,值);
}
}

您可以通过
字段Field=getClass().getDeclaredField(name)为您的名字获取一个
字段

然后看看Java文档: 您可以使用多个选项根据数据类型设置变量的值

看看这个例子:编辑:根据您的确切问题进行更新

import java.lang.reflect.Field;
import java.util.Map;
import java.util.HashMap;

public class FieldTest {

    String name;
    String id;


    public static void main(String[] args) {
        new FieldTest();
    }

    public FieldTest() {
    Map<String,String> variablesMap = new HashMap<String,String>();
    variablesMap.put("name","Joshi");
    variablesMap.put("id","101");

    for (Map.Entry<String, String> entry : variablesMap.entrySet())
    {
        try {
        test(entry.getKey(),entry.getValue());
        }
        catch(NoSuchFieldException ex) {
            ex.printStackTrace();
        }
        catch(IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    System.out.println(name);
    System.out.println(id);
    }

    private void test(String name, String value) throws NoSuchFieldException, IllegalAccessException {
        Field field = getClass().getDeclaredField(name);
        field.set(this,value);
    }

}
import java.lang.reflect.Field;
导入java.util.Map;
导入java.util.HashMap;
公开课实地测试{
字符串名;
字符串id;
公共静态void main(字符串[]args){
新现场测试();
}
公共现场测试(){
Map variablesMap=newhashmap();
可变映射put(“名称”、“Joshi”);
可变映射放置(“id”,“101”);
对于(Map.Entry:variablesMap.entrySet())
{
试一试{
测试(entry.getKey(),entry.getValue());
}
捕获(NoSuchFieldException-ex){
例如printStackTrace();
}
捕获(非法访问例外e){
e、 printStackTrace();
}
}
System.out.println(名称);
系统输出打印项次(id);
}
私有void测试(字符串名称、字符串值)抛出NoSuchFieldException、IllegalAccessException{
Field=getClass().getDeclaredField(名称);
字段.set(此字段,值);
}
}

您可以通过
字段Field=getClass().getDeclaredField(name)为您的名字获取一个
字段

然后看看Java文档: 您可以使用多个选项根据数据类型设置变量的值

看看这个例子:编辑:根据您的确切问题进行更新

import java.lang.reflect.Field;
import java.util.Map;
import java.util.HashMap;

public class FieldTest {

    String name;
    String id;


    public static void main(String[] args) {
        new FieldTest();
    }

    public FieldTest() {
    Map<String,String> variablesMap = new HashMap<String,String>();
    variablesMap.put("name","Joshi");
    variablesMap.put("id","101");

    for (Map.Entry<String, String> entry : variablesMap.entrySet())
    {
        try {
        test(entry.getKey(),entry.getValue());
        }
        catch(NoSuchFieldException ex) {
            ex.printStackTrace();
        }
        catch(IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    System.out.println(name);
    System.out.println(id);
    }

    private void test(String name, String value) throws NoSuchFieldException, IllegalAccessException {
        Field field = getClass().getDeclaredField(name);
        field.set(this,value);
    }

}
import java.lang.reflect.Field;
导入java.util.Map;
导入java.util.HashMap;
公开课实地测试{
字符串名;
字符串id;
公共静态void main(字符串[]args){
新现场测试();
}
公共现场测试(){
Map variablesMap=newhashmap();
可变映射put(“名称”、“Joshi”);
可变映射放置(“id”,“101”);
对于(Map.Entry:variablesMap.entrySet())
{
试一试{
测试(entry.getKey(),entry.getValue());
}
捕获(NoSuchFieldException-ex){
例如printStackTrace();
}
捕获(非法访问例外e){
e、 printStackTrace();
}
}
System.out.println(名称);
系统输出打印项次(id);
}
私有void测试(字符串名称、字符串值)抛出NoSuchFieldException、IllegalAccessException{
Field=getClass().getDeclaredField(名称);
字段.set(此字段,值);
}
}
getClass().getDeclaredField(myFieldName).set(myObject, myValue);
String methodName = "set" + myFieldName.subString(0, 1).toUpperCase() + myFieldName.subString(1);
Method method = getClass().getMethod(methodName, String.class);
method.invoke(myObject, myValue);