开发者

How to test if interface Foo<Bar> is "assignable" from Baz?

Code:

System.out.println(Compara开发者_开发百科ble.<Integer>isAssignableFrom(one.getClass()));

Error:

comp.java:16: cannot find symbol
symbol  : method <java.lang.Integer>isAssignableFrom(java.lang.Class<capture#128
of ? extends java.lang.Integer>)
location: interface java.lang.Comparable
    System.out.println(Comparable.<Integer>isAssignableFrom(one.getClass()));

How can I determine whether some given type can be passed as an argument of type SomeInterface?

PS: Is there a Java reference manual/programmer's handbook that would have given me the answer to this question in reasonable time (i.e. without requiring reading thousands of pages of irrelevant verbiage)? At the moment I find myself programming Java by trial-and-error, which really sucks.


Due to type erasure, it's not possible to test if a particular object is an instance of a specific generic type (which it looks like is what you're trying to do here). The closest you can get is to test if the object is an instance of the raw Foo type:

System.out.println(one instanceof Comparable);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜