由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - An interesting thing about java generics-do not laugh at me if u think it too basic
相关主题
[转载] Java 1.5 Generic 问题Generics应该怎么得到它的class?
请问有没有generic的arraySun的JDK 1.5有什么新特点吗?
问个 Generic 的问题1999年出版的C,C++,Java语言教程过期了吗??
a stupid questionInteresting discussion: java and generics and more
Java里面有没有可能写个带generic parameter的class对built-in type也适用?how to do this?
Interview的人问我最新java 版本是多少scala - I 服了 U
如何定义这样的数组?oracle真黑啊
问一个Java的问题,关于create generic arrayextending generic class , but not mentioning its parameterized type?
相关话题的讨论汇总
话题: base话题: string话题: class话题: generics话题: parameter
进入Java版参与讨论
1 (共1页)
h*********o
发帖数: 62
1
public class Base {
private Class persistentClass;

public Base() {
Class clazz = getClass();
this.persistentClass = (Class) ((ParameterizedType)
clazz.getGenericSuperclass()).getActualTypeArguments()[0];

}
}
It seems that there is no way you can use it as follows:
Base b = new Base();
to use it you have to define a subclass specifying the type parameter just
as you do in C++ template.
BaseDerative extends Base....
NOT:
m******t
发帖数: 2416
2
Interesting... note though that the "String" you get is not the "String"
from "BaseDeritive", but the one from "Base".
In other words, it'll work even with "BaseDeritive extends Base".
h*********o
发帖数: 62
3
rite.
That is exactly how Hibernate does.
Do not know under hood how jvm handles this thing.
F****n
发帖数: 3271
4
It's not a JVM implementation issue. It's simply the way Generics should be.
Remeber Java Generics is a compile time thing rather than runtime thing. In
other words, if you declare Base, then the parameter of this class is a
TypeVariable.
Even if you define a new Base variable, its type parameter is still
a TypeVariable because Base only help you check type at compile type
. It will not changed the parameter of Base into a String.
Consequently, your original code will throw

【在 h*********o 的大作中提到】
: public class Base {
: private Class persistentClass;
:
: public Base() {
: Class clazz = getClass();
: this.persistentClass = (Class) ((ParameterizedType)
: clazz.getGenericSuperclass()).getActualTypeArguments()[0];
:
: }
: }

1 (共1页)
进入Java版参与讨论
相关主题
extending generic class , but not mentioning its parameterized type?Java里面有没有可能写个带generic parameter的class对built-in type也适用?
菜鸟问关于Java Programming的问题Interview的人问我最新java 版本是多少
generics这样改对马?如何定义这样的数组?
how to update swing components问一个Java的问题,关于create generic array
[转载] Java 1.5 Generic 问题Generics应该怎么得到它的class?
请问有没有generic的arraySun的JDK 1.5有什么新特点吗?
问个 Generic 的问题1999年出版的C,C++,Java语言教程过期了吗??
a stupid questionInteresting discussion: java and generics and more
相关话题的讨论汇总
话题: base话题: string话题: class话题: generics话题: parameter