简体   繁体   English

如何测试接口Foo <Bar> 来自巴兹的“可转让”?

[英]How to test if interface Foo<Bar> is “assignable” from Baz?

Code: 码:

System.out.println(Comparable.<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? 如何确定某些给定类型是否可以作为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.

相关问题 新的Foo(bar)是什么{public void baz(){…}}; 在Java中意味着什么? - What does new Foo(bar) {public void baz(){…} }; mean in Java? 创建可以像这样调用的java方法:foo.bar()。baz()。quux() - Creating java methods that can be called like this: foo.bar().baz().quux() 如何获得Flowable <List<Foo> &gt;使用领域和Rxjava从具有一对多关系&#39;Foo&#39;的&#39;Bar&#39;对象 - How to get Flowable<List<Foo>> from 'Bar' Object with One-to-many relations 'Foo' using realm and Rxjava 如何测试Actor Foo向新创建的儿童演员Bar发送消息? - How to test that Actor Foo sends messages to newly created child actors Bar? 将“select foo from Foo foo, Bar bar where ...”形式的HQL查询转换为Criteria查询 - Convert HQL query in the form of “select foo from Foo foo, Bar bar where …” to Criteria query 如何用foo或bar检查一行? - how to check a line start with foo or bar? 如何将我的foo.war的根路径设置为/ foo / bar而不是/ foo? - How do I set my foo.war's root path to be /foo/bar instead of /foo? 最简洁的方法来确定列表 <Foo> 包含Foo.getBar()=“Baz”的元素? - Most Concise Way to Determine List<Foo> Contains Element Where Foo.getBar() = “Baz”? 为什么不能从Comparable接口分配Java的String - Why Java's String is not assignable from Comparable interface 键入不安全:未经检查,从Foo投射到Bar。 JPA的东西 - Type unsafety: Unchecked cast from Foo to Bar. JPA stuff
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM