将字符串值转换为变量名称|字符串值包含变量名称 [英] convert string value to variable name | string value contain variable name

查看:138
本文介绍了将字符串值转换为变量名称|字符串值包含变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Double new_val = 10.0; 
String a =new;
String b =val;
Double v1 = 25.0;
双重结果= 0.0;

public void getVal(){
//字符串变量c包含双重变量名称
字符串c = a +_+ b;
//我想将c的值设为10.0,因为它已经定义了一个变量
result = v1 * c;





lock $ $ b $ p $如果问题是,你是否可以,并用于进一步



解决方案

strong>获取知道运行时名称的变量的值,然后是好消息....是的,当然可以......您需要执行一些名为 REFFLECTION ...



允许您作为开发者制作一个在你的情况下,你需要找到一个变量(或字段)的名称,甚至浏览该类所持有的所有信息



并阅读它的价值...

查看文档以获取更多信息,并且我会建议您考虑是否真的需要这样做......通常是反射意图当你想从另一个类访问信息而不是浏览自己时使用它。



你可以重新设计一些应用程序并定义一些常量和方法,这样其他人就可以看到你正在向他们展示什么,并使他们可用......



示例:



  public class Jung {
Double new_val = 10.0;
String a =new;
String b =val;
Double v1 = 25.0;
双重结果= 0.0;

public void getVal(){
//字符串变量c包含双重变量名称
字符串c = a +_+ b;
Double cAsVal = 0.0;
尝试{
cAsVal = dale(c);
} catch(NoSuchFieldException e){
e.printStackTrace();
} catch(SecurityException e){
e.printStackTrace();
} catch(IllegalArgumentException e){
e.printStackTrace();
} catch(IllegalAccessException e){
e.printStackTrace();
}
result = v1.doubleValue()* cAsVal.doubleValue();
System.out.println(result);
}

public static void main(String [] args){
Jung j = new Jung();
j.getVal();

$ b $ public double dale(String c)
throws NoSuchFieldException,SecurityException,IllegalArgumentException,IllegalAccessException {
Field field = this.getClass()。getDeclaredField(c) ;
field.setAccessible(true);
Object value = field.get(this);
return(Double)value;
}
}


Double new_val = 10.0;
String a = "new";
String b = "val";
Double v1 = 25.0;
Double result = 0.0;

Public void getVal() {
    //String variable c contain double variable name
    String c = a+"_"+b;
    //I want to get c's value as 10.0 as its a variable already defined
    result = v1*c;
}

"c" String value contain variable name "new_val" and that is used for further

解决方案

If the question is, whether you can get the value of a variable knowing its name at runtime, then good news.... Yes for sure you can... you will need to do something called REFFLECTION...

which is allowing you as developer to make an instrocpection of the class and even "browse" all the info that the class is holding

in your case you need to find a "variable" (or Field) by the name and read its value...

look the doc for more info, and I would recommend you to consider if you really need to do this... normally reflection is intended to be used when you want to access info from another class and not about browsing yourself...

you can maybe redesign a little the application and define some constants and methods so other can see what you are exposing to them and making for them available...

Example:

public class Jung {
Double new_val = 10.0;
String a = "new";
String b = "val";
Double v1 = 25.0;
Double result = 0.0;

public void getVal() {
    // String variable c contain double variable name
    String c = a + "_" + b;
    Double cAsVal = 0.0;
    try {
        cAsVal = dale(c);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    result = v1.doubleValue() * cAsVal.doubleValue();
    System.out.println(result);
}

public static void main(String[] args) {
    Jung j = new Jung();
    j.getVal();
}

public Double dale(String c)
    throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Field field = this.getClass().getDeclaredField(c);
    field.setAccessible(true);
    Object value = field.get(this);
    return (Double) value;
}
}

这篇关于将字符串值转换为变量名称|字符串值包含变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆