1

它将如何使用以下打印欢迎System.out.println。生成会给出ac。但是我应该如何使它成为welcome(即 ia ac 值不是"ac"

public class BrowserSample {

  public static void main(String[] args) {    
    String generated = "ac";
    String ac = "welcome";
    System.out.println("value from generated is = " + generated);
  }
}
4

2 回答 2

2

好的经过简短的实验,这里是您想要的解决方案

String generated = "ac";

String ac = "welcome";  // declare as member of class

String s = (String) getClass().getDeclaredField(generated).get(this);

s 将包含welcome

于 2013-05-17T07:34:56.743 回答
1

地图,键值对怎么样。

Map<String,String> map = new HashMap<>();
String generated = "ac";
map.put("ac","welcome")
System.out.println("value from generated is = "+map.get("ac"));

而你所期望的那是不可能的,也是毫无意义的。

于 2013-05-17T07:25:31.840 回答