由买买提看人间百态

topics

全部话题 - 话题: getobjectclass
(共0页)
l*******w
发帖数: 61
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 ... 阅读全帖
l*******w
发帖数: 61
2
见 //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 ... 阅读全帖
f*******n
发帖数: 12623
3
No, all generics are erased after compiling. There is no T. Anything you can
get you must get from stuff that you have at runtime.
Think about it, after type erasure your class looks like this:
public class GenericReflection
{
private Object object;
//---- get/set ---
public void setObject( Object v ){ object=v; }
public Object getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class getObjectClass()
{
Class retval = null;

if... 阅读全帖
f*******n
发帖数: 12623
4
No, all generics are erased after compiling. There is no T. Anything you can
get you must get from stuff that you have at runtime.
Think about it, after type erasure your class looks like this:
public class GenericReflection
{
private Object object;
//---- get/set ---
public void setObject( Object v ){ object=v; }
public Object getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class getObjectClass()
{
Class retval = null;

if... 阅读全帖
(共0页)