简体   繁体   English

如何从Java中的对象获取运行时类型

[英]How to obtain the runtime type from an object in java

I have this code: 我有以下代码:

public void doSomething(Integer param){
.
.
.
  JAXBElement<Integer> jaxb = new JAXBElement<Integer>
                                    (new QName("uri","local"),Integer.class, param);
.
.
.
}

And I want it to be more flexible. 我希望它更加灵活。 I need the parameter to be of type Object, but I don't know how to change this line: 我需要参数的类型为Object,但是我不知道如何更改此行:

JAXBElement< what_do_I_put_here > jaxb = new JAXBElement< what_do_I_put_here >(new QName("uri","local"),Integer.class, param); JAXBElement < what_do_I_put_here > jaxb = new JAXBElement < what_do_I_put_here >(new QName(“ uri”,“ local”),Integer.class,param);

I want it to use the runtime class of the type stored in param 我希望它使用存储在param中的类型的运行时类

public void doSomething(Object param){
.
.
.
  JAXBElement<??????> jaxb = new JAXBElement<??????>
                                    (new QName("uri","local"),?????.class, param);
.
.
.
}

Not entirely sure (don't have access to an IDE at the moment) but isn't is something like 不确定(目前无法访问IDE),但不是这样

public <T extends Object> void doSomething(T param) {
    .
    .
    JAXBElement<T> jaxb = new JAXBElement<T>(new QName("uri", "local"), (Class<T>) param.getClass(), param);
    .
    .
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM