如何获取泛型的类型啊 T.class

-droidcoffee- 2011-01-19 09:47:31

public class MyList<T>{
public static void main(String[] args){

}
}



请问一下,
运行时 我该如何在获取 MyList<T> 中T的 数据类型啊???

...全文
1797 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mtv0199 2011-01-20
  • 打赏
  • 举报
回复
2楼正解
泛型的存在就是把强制转化可能引发的错误提前到编译期检测,运行时使用泛型要传入一个参数T,在类里面写个方法返回传入参数的类型就可以了。
absolutej 2011-01-20
  • 打赏
  • 举报
回复
Type
  • 打赏
  • 举报
回复
可以关注一下这个贴子,
-droidcoffee- 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hackerster0324 的回复:]
public class MainTest {
public static void main(String[] args) {
Test<String> t = new Test<String>("a");
t.test();
Test<Integer> t1 = new Test<Integer>(1);
t1.test();
}
}

class Test<T> {
pr……
[/Quote]

2楼 方法指标不治本,如果对方提供的方法里面没有响应方法, 那岂不是也无法取到?

3楼那篇日志
看了一下
======================
这是泛型擦拭法使得Generic无法获取自己的Generic Type类型。实际上BadClass<String>()实例化以后Class里面就不包括T的信息了,对于Class而言T已经被擦拭为Object,而真正的T参数被转到使用T的方法(或者变量声明或者其它使用T的地方)里面(如果没有那就没有存根),所以无法反射到T的具体类别,也就无法得到T.class。而getGenericSuperclass()是Generic继承的特例,对于这种情况子类会保存父类的Generic参数类型,返回一个ParameterizedType,这时可以获取到父类的T.class了,这也正是子类确定应该继承什么T的方法

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ykdsg/archive/2010/04/11/5472591.aspx



难道获取T.class 就只能通过子类来搞定么?

如果不继承就真的没法获取???

-droidcoffee- 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 shine333 的回复:]
引用 8 楼 arkwrightzhn 的回复:
correct. Generic is used to help you correct your error while compiling. if you want to get the runtime type. you'll have to think your own ways. for example, to make up a me……
[/Quote]

哥们 看来只能用这种办法了,

待会结贴

谢谢各位了
shine333 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 arkwrightzhn 的回复:]
correct. Generic is used to help you correct your error while compiling. if you want to get the runtime type. you'll have to think your own ways. for example, to make up a method.
[/Quote]

+1,继承的那种方法是因为在编译期,class B extends A<String>, class C extends A<Integer>的信息已经能够获取,明显知道是String/Integer。所以相关的getClass()方法才会带有各自的泛型信息。
-droidcoffee- 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 iambic 的回复:]
没明白你要干啥。解决问题都是具体问题具体分析,反映到具体问题,不少人一辈子也碰不上真正无法解决的问题,但是要回答这种泛泛的帖子,十个倒有八个是无解的,因为根本就不是一个实在的问题。
[/Quote]
哥们 有些问题你想怎么实在?

是这样子么? 呵呵
---------------


class User(){
}
class MyList<T>{
}

public class Test{
public static void main(String)[
MyList<User> us = new MyList(User);

// 如何通过us获取 T 的类型?

}
}



dracularking 2011-01-20
  • 打赏
  • 举报
回复
Erasure表现在这里就像这样:

public String loophole(Integer x) {
List<String> ys = new LinkedList<String>();
List xs = ys;
xs.add(x); // compile-time unchecked warning
return ys.iterator().next();
}

通过类型擦除转化为:

public String loophole(Integer x) {
List ys = new LinkedList;
List xs = ys;
xs.add(x);
return (String) ys.iterator().next(); // run time error
}

但是最后强制转换时,String类型信息不是恰恰存在着的嘛,可能没有一种很直接的方式来获得这个信息
iambic 2011-01-20
  • 打赏
  • 举报
回复
没明白你要干啥。解决问题都是具体问题具体分析,反映到具体问题,不少人一辈子也碰不上真正无法解决的问题,但是要回答这种泛泛的帖子,十个倒有八个是无解的,因为根本就不是一个实在的问题。
Norris_Zhang 2011-01-20
  • 打赏
  • 举报
回复
correct. Generic is used to help you correct your error while compiling. if you want to get the runtime type. you'll have to think your own ways. for example, to make up a method.
swoky 2011-01-19
  • 打赏
  • 举报
回复
楼上正解

看下这个
http://blog.csdn.net/ykdsg/archive/2010/04/11/5472591.aspx
hackerster0324 2011-01-19
  • 打赏
  • 举报
回复
public class MainTest {
public static void main(String[] args) {
Test<String> t = new Test<String>("a");
t.test();
Test<Integer> t1 = new Test<Integer>(1);
t1.test();
}
}

class Test<T> {
private T t;
public Test(T t) {
this.t = t;
}
public void test() {
System.out.println(t.getClass());
}
}
龙四 2011-01-19
  • 打赏
  • 举报
回复
泛型只是在编译时有效

运行时是通过强制转换来做的

你反编译一下源码就会发现,泛型的信息在.clsss文件中已经被擦除了

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧