[英]How to test if interface Foo<Bar> is “assignable” from Baz?
System.out.println(Comparable.<Integer>isAssignableFrom(one.getClass()));
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? 如何确定某些给定类型是否可以作为SomeInterface类型的参数传递?
PS: Is there a Java reference manual/programmer's handbook that would have given me the answer to this question in reasonable time (ie without requiring reading thousands of pages of irrelevant verbiage)? PS:是否有一本Java参考手册/程序员手册可以在合理的时间内给我这个问题的答案(即不需要阅读数千页不相关的词汇)? At the moment I find myself programming Java by trial-and-error, which really sucks. 目前我发现自己通过反复试验编写Java,这真的很糟糕。
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: 您可以获得的最接近的是测试对象是否是原始Foo
类型的实例:
System.out.println(one instanceof Comparable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.