论坛首页 入门技术论坛

为什么运行时取不到泛型T,难道你们觉得没必要吗

浏览 15110 次
该帖已经被评为新手帖
作者 正文
   发表时间:2012-02-01  
gtssgtss 写道
vcok 写道
在编译期间T被搞成Object,类型丢失,所以是不能直接取到的.但有时可以变通地取到,比如: 

public class GenericType<T> {
    protected GenericType() {
        Type superclass = getClass().getGenericSuperclass();
        if (!(superclass instanceof ParameterizedType)) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
       
        Class<T> messageType = parameterized.getActualTypeArguments()[0];
    }

一般人俺不告诉他


new GenericType<String>();取不到
GenericTypeString extends GenericType<String> new GenericTypeString();才取得到


我这里没必要再继承一个类似GenericType<String>的东西吧。
0 请登录后投票
   发表时间:2012-02-01  
因为Java的泛型只是对编译期有效,运行期不存在泛型的,相应的T类型在运行时其实依然是java.lang.Object类型。
0 请登录后投票
   发表时间:2012-02-01  
gtssgtss 写道
既然是api要用Class的话,那自然需要咯
实在要取的话,看看这个
http://www.iteye.com/topic/585900
你会发现,你取得到的时候没必要,有必要的时候取不到--


我通过xx.class.getTypeParameters();获取出来的数组长度为0;文章里说,使用该方法可获取类型T
0 请登录后投票
   发表时间:2012-02-01  
H_eaven 写道
因为Java的泛型只是对编译期有效,运行期不存在泛型的,相应的T类型在运行时其实依然是java.lang.Object类型。

谢谢
0 请登录后投票
   发表时间:2012-02-01  
vcok 写道
在编译期间T被搞成Object,类型丢失,所以是不能直接取到的.但有时可以变通地取到,比如: 

public class GenericType<T> {
    protected GenericType() {
        Type superclass = getClass().getGenericSuperclass();        if (!(superclass instanceof ParameterizedType)) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
       
        Class<T> messageType = parameterized.getActualTypeArguments()[0];
    }

一般人俺不告诉他

把红色的改成如下:Type superInterface=getClass().getGenericInterfaces()试试,应该就可以吧。
0 请登录后投票
   发表时间:2012-02-01  
laolinshi 写道
vcok 写道
在编译期间T被搞成Object,类型丢失,所以是不能直接取到的.但有时可以变通地取到,比如: 

public class GenericType<T> {
    protected GenericType() {
        Type superclass = getClass().getGenericSuperclass();        if (!(superclass instanceof ParameterizedType)) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
       
        Class<T> messageType = parameterized.getActualTypeArguments()[0];
    }

一般人俺不告诉他

把红色的改成如下:Type superInterface=getClass().getGenericInterfaces()试试,应该就可以吧。


兄弟不行的,这根interfaces有啥关系呢。
0 请登录后投票
   发表时间:2012-02-01  
我想正如 http://www.iteye.com/topic/585900  这篇文章里提到的

如果这么做
List<Object> list = new ArrayList<Object>();
list.add(1);
list.add("");

该如何在运行时获取Object的类型
0 请登录后投票
   发表时间:2012-02-01  
icezx 写道
laolinshi 写道
vcok 写道
在编译期间T被搞成Object,类型丢失,所以是不能直接取到的.但有时可以变通地取到,比如: 

public class GenericType<T> {
    protected GenericType() {
        Type superclass = getClass().getGenericSuperclass();        if (!(superclass instanceof ParameterizedType)) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
       
        Class<T> messageType = parameterized.getActualTypeArguments()[0];
    }

一般人俺不告诉他

把红色的改成如下:Type superInterface=getClass().getGenericInterfaces()试试,应该就可以吧。


兄弟不行的,这根interfaces有啥关系呢。

你不是定义一个MybatisCallBack<T>的接口吗,你的实现类实现了这个接口的话,上面的方法就可以取到这个T的实际类型。
0 请登录后投票
   发表时间:2012-02-01  
楼主看一下这个博客 http://www.blogjava.net/calvin/archive/2006/04/28/43830.html
0 请登录后投票
   发表时间:2012-02-01  
icezx 写道
我想正如 http://www.iteye.com/topic/585900  这篇文章里提到的

如果这么做
List<Object> list = new ArrayList<Object>();
list.add(1);
list.add("");

该如何在运行时获取Object的类型

用List<Object> list = new ArrayList<Object>(){};就可以取了,不过与其用如此晦涩的方法,还不如直接传一个Class
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics