这是我正在努力的一段代码。正确的java语法与扩展的泛型相关
public class Channel<T extends Something>{
public Channel(){}
public void method(T something){}
}
public class Manager{
private static ArrayList<Channel<? extends Something>> channels
= new ArrayList<Channel<? extends Something>>();
public static <T extends Something> void OtherMethod(T foo){
for(Channel<? extends Something> c : channels)
c.method(foo); // this does not work
}
}
行不行给我的编译器错误:
The method method(capture#1-of ? extends Something) in the type Channel<capture#1-of ? extends Something> is not applicable for the arguments (T)
我不明白这个错误。如果我删除了Manager类中的所有泛型,它正在工作,但输入不安全。 我应该如何在正确的Java中执行此操作?
从未发生过。我简化了代码。 OtherMethod会在他的频道中搜索频道,并使用它。请不要告诉我,我的代码不是很优雅。请告诉我为什么这是不正确的。 –
ArcticLord
2014-12-03 15:21:55
@ ArcticLord:你的代码被写入的方式,这是不正确的。如果你没有显示你的实际代码,我不能帮你。 – SLaks 2014-12-03 15:24:42