Is it possible to get Class object for T from a generic class? (下列空档,是否可填)# Java - 爪哇娇娃
l*w
1 楼
见 //Q:处。
我想不出什么高招可以完成 getObjectClass() ... 惭愧。。。也许是做不到的?
请教各位高见。谢谢。
------------------------------
public class GenericReflection
{
private T object;
//---- get/set ---
public void setObject( T v ){ object=v; }
public T getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class getObjectClass()
{
Class retval = null;
if( object!=null ) retval = (Class) object.getClass();
else
{
//Q: can the class be determined when object is null,
// thru some kind of reflection?
}
return retval;
}//end getObjectClass(.)
}//end class GenericReflection
我想不出什么高招可以完成 getObjectClass() ... 惭愧。。。也许是做不到的?
请教各位高见。谢谢。
------------------------------
public class GenericReflection
{
private T object;
//---- get/set ---
public void setObject( T v ){ object=v; }
public T getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class
{
Class
if( object!=null ) retval = (Class
else
{
//Q: can the class be determined when object is null,
// thru some kind of reflection?
}
return retval;
}//end getObjectClass(.)
}//end class GenericReflection