13
java:如何将变量从一种类型动态转换为另一种类型?
我想对Java变量进行动态转换,转换类型存储在其他变量中。 这是常规转换: String a = (String) 5; 这就是我要的: String theType = 'String'; String a = (theType) 5; 这有可能吗?谢谢! 更新资料 我正在尝试使用HashMap收到的A填充类。 这是构造函数: public ConnectParams(HashMap<String,Object> obj) { for (Map.Entry<String, Object> entry : obj.entrySet()) { try { Field f = this.getClass().getField(entry.getKey()); f.set(this, entry.getValue()); /* <= CASTING PROBLEM */ } catch (NoSuchFieldException ex) { …