java:我怎么能动态转换变量从一种类型到另一种? [英] java: how can i do dynamic casting of a variable from one type to another?

查看:206
本文介绍了java:我怎么能动态转换变量从一种类型到另一种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是正规投射:

$

我想为一个java变量执行动态转换b
$ b

  String a =(String)5; 

这就是我想要的:

  String theType ='String'; 
String a =(theType)5;

有可能吗?如果是这样怎么办?谢谢!



更新



我试图用接收到的hashMap填充类。 p>

这是构造函数:

  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){
log.error(did not find field'+ entry.getKey()+'');
} catch(IllegalAccessException ex){
log.error(ex.getMessage());
}
}

}

这里的问题是一些类变量是Double类型,如果接收到数字3,它看到它为Integer,类型问题。

解决方案


关于更新,在Java中解决这个问题的唯一方法是写代码覆盖所有包含如果 else instanceof 表达式,你试图做的看起来像是用来编程动态语言在静态语言中,你试图做的几乎是不可能的,你可能会选择一个完全不同的方法,你试图做的静态语言只是没有动态的那样灵活)。



Java最佳实践的好例子是由BalusC回答(即 ObjectConverter )和

>




中没有意义

  String a =(theType)5; 

的类型 String ,因此对这个静态类型进行动态转换没有任何意义。



PS:您的示例的第一行可以写为 Class< String> stringClass = String.class; 但仍然不能使用 stringClass 来投射变量。


i would like to do dynamic casting for a java variable, the casting type is stored in a different variable.

this is regular casting:

 String a = (String) 5;

this is what i want:

 String theType = 'String';
 String a = (theType) 5;

is it possible? and if so how? thanks!

update

I'm trying to populate a class with a hashMap that i received.

this is the constructor:

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) {
            log.error("did not find field '" + entry.getKey() + '"');
        } catch (IllegalAccessException ex) {
            log.error(ex.getMessage());         
        }
    }

}

the problem here is that some of the classes variables are Double type, and if the number 3 is received it sees it as Integer and i have type problem.

解决方案

Regarding your update, the only way to solve this in Java is to write code that covers all cases with lots of if and else and instanceof expressions. What you attempt to do looks as if are used to program with dynamic languages. In static languages, what you attempt to do is almost impossible and one would probably choose a totally different approach for what you attempt to do. Static languages are just not as flexible as dynamic ones :)

Good examples of Java best practice are the answer by BalusC (ie ObjectConverter) and the answer by Andreas_D (ie Adapter) below.


That does not make sense, in

String a = (theType) 5;

the type of a is statically bound to be String so it does not make any sense to have a dynamic cast to this static type.

PS: The first line of your example could be written as Class<String> stringClass = String.class; but still, you cannot use stringClass to cast variables.

这篇关于java:我怎么能动态转换变量从一种类型到另一种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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