protected Tmp() { Type superClass = getClass().getGenericSuperclass(); if (superClass instanceof Class<?>) { // sanity check, should never happen throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information"); } } public static void main(String[] args) { Tmp t = new Tmp<Object>(); } jdk1.6实测,if里返回的是trueException in thread "main" java.lang.IllegalArgumentException: Internal error: TypeReference constructed without actual type information
兄台, OptionManager manager = new OptionManager(); 调试的结果是if为false 不是吗? 我想知道原因。
看错了代码。原因很简单,因为 Type superClass不是Class的子类,所以更加不可能是Class的instance
yangweihong20110929 说的很白了,这是 class的代码: public Type getGenericSuperclass() { if (getGenericSignature() != null) { // Historical irregularity: // Generic signature s interfaces with superclass = Object // but this API returns null for interfaces if (isInterface()) return null; return getGenericInfo().getSuperclass(); } else return getSuperclass(); } 如果具有泛型签名,返回的是ParameterizedType接口,父接口是Type接口,但不是Class对象。这个时候instanceof Class<?>是为false的。 如果没有泛型签名,返回的是Class对象,Class对象也实现了Type接口。这个时候instanceof Class<?>是为true的。 ..... 我没想明白的就是获取类型参数为什么要通过getGenericSuperclass();来过渡一下?
OptionManager manager = new OptionManager();
调试的结果是if为false
//GenericDAO<MSGC_OPTION> genericDAO = new GenericDAO<MSGC_OPTION>();
调试的结果是if为true
能再讲清楚点吗?比如我想知道Class<?>,什么才是instance of Class<?>?
谢谢!
如果一个类的对象是A,没有泛型,而要比较的是B,也没有泛型,并且B是从A继承的,那么返回就是true,如果B有泛型,A没有,则肯定返回false,OptionManager manager = new OptionManager();这句肯定正确,在调试下试试,因为Type superClass = getClass().getGenericSuperclass();获取的对象superClass 是GenericDAO<MSGC_OPTION>,符合class<?>这种形式,并且class和GenericDAO这两个对象一样,而且泛型一样,但是GenericDAO<MSGC_OPTION> genericDAO = new GenericDAO<MSGC_OPTION>();肯定false,原因是superClass 是Object,没有泛型,所以即使Object和class一样,但是没有泛型,所以肯定false。如果你将class<?>的泛型去掉,那肯定就是你上面说的情况,如果还没明白可以继续问,正好今天没什么事,可以多回答点给你。
我真心的调试了啊
我们先解决哪个是true,哪个是false吧。
OptionManager manager = new OptionManager();
得到的Type superClass = getClass().getGenericSuperclass();是GenericDAO<MSGC_OPTION>
而//GenericDAO<MSGC_OPTION> genericDAO = new GenericDAO<MSGC_OPTION>();
得到的Type superClass = getClass().getGenericSuperclass();是Object
这个是和你说的一致的,这部分我能够理解,就是直接的父类类型。
我真心的调试了啊
我们先解决哪个是true,哪个是false吧。
OptionManager manager = new OptionManager();
得到的Type superClass = getClass().getGenericSuperclass();是GenericDAO<MSGC_OPTION>
而//GenericDAO<MSGC_OPTION> genericDAO = new GenericDAO<MSGC_OPTION>();
得到的Type superClass = getClass().getGenericSuperclass();是Object
这个是和你说的一致的,这部分我能够理解,就是直接的父类类型。
额,抱歉,我要下班了,回家看看,明天在看看,也许是我哪里没看清楚。。
我真心的调试了啊
我们先解决哪个是true,哪个是false吧。
OptionManager manager = new OptionManager();
得到的Type superClass = getClass().getGenericSuperclass();是GenericDAO<MSGC_OPTION>
而//GenericDAO<MSGC_OPTION> genericDAO = new GenericDAO<MSGC_OPTION>();
得到的Type superClass = getClass().getGenericSuperclass();是Object
这个是和你说的一致的,这部分我能够理解,就是直接的父类类型。
额,抱歉,我要下班了,回家看看,明天在看看,也许是我哪里没看清楚。。已经非常感谢你了,这么有耐心的帮我。
protected Tmp() {
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof Class<?>) { // sanity check, should never happen
throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
}
}
public static void main(String[] args) {
Tmp t = new Tmp<Object>();
}
jdk1.6实测,if里返回的是trueException in thread "main" java.lang.IllegalArgumentException: Internal error: TypeReference constructed without actual type information
兄台,
OptionManager manager = new OptionManager();
调试的结果是if为false
不是吗?
我想知道原因。
Type superClass不是Class的子类,所以更加不可能是Class的instance
你的说法和我的一直以来的理解有偏差我认为,所有的类都只是Class的对象,没有Class的子类或类这一说。所以我不是很明白你所谓的“Type superClass不是Class的子类”是什么意思
你的说法和我的一直以来的理解有偏差我认为,所有的类都只是Class的对象,没有Class的子类或类这一说。所以我不是很明白你所谓的“Type superClass不是Class的子类”是什么意思
你的说法和我的一直以来的理解有偏差我认为,所有的类都只是Class的对象,没有Class的子类或类这一说。所以我不是很明白你所谓的“Type superClass不是Class的子类”是什么意思你理论学歪了,所有类都是Object的子类,Class是用来描述类信息的
居然没看懂我发现我对于Class这一块算不上懂。
public Type getGenericSuperclass() {
if (getGenericSignature() != null) {
// Historical irregularity:
// Generic signature s interfaces with superclass = Object
// but this API returns null for interfaces
if (isInterface())
return null;
return getGenericInfo().getSuperclass();
} else
return getSuperclass();
}
如果具有泛型签名,返回的是ParameterizedType接口,父接口是Type接口,但不是Class对象。这个时候instanceof Class<?>是为false的。
如果没有泛型签名,返回的是Class对象,Class对象也实现了Type接口。这个时候instanceof Class<?>是为true的。
.....
我没想明白的就是获取类型参数为什么要通过getGenericSuperclass();来过渡一下?