我想了解为什么这段代码不会编译。
我有一个类实现一个接口。由于某种原因,最后的方法不会编译。Java抛出与SortedSet异常
它不会简单地允许我将集合转换为集合,但确实允许它返回单个对象。
有人可以向我解释为什么这是?谢谢。
public class Testing2 {
public SortedSet<ITesting> iTests = new TreeSet<ITesting>();
public SortedSet<Testing> tests = new TreeSet<Testing>();
public ITesting iTest = null;
public ITesting test = new Testing();
// Returns the implementing class as expected
public ITesting getITesting(){
return this.test;
}
// This method will not compile
// Type mismatch: cannot convert from SortedSet<Testing> to SortedSet<ITesting>
public SortedSet<ITesting> getITests(){
return this.tests;
}
}
你会编辑你的问题,包括确切的编译器信息吗?编辑:另外,它看起来像测试实现ITesting? – 2013-02-28 21:44:02
是的,对不起。测试实现ITesting – Marley 2013-02-28 21:48:05
看起来像http://stackoverflow.com/questions/897935/when-do-java-generics-require-extends-t-instead-of-t-and-is-there-any-down的副本 – 2013-02-28 21:49:36