Tag: covariant

接口中的协变返回类型不通过Javac进行编译

我有以下结构: public interface BarReturn {} public interface FooReturn {} public interface FooBarReturn extends FooReturn, BarReturn {} public interface Foo { FooReturn fooBar( ); } public interface Bar { BarReturn fooBar(); } public interface FooBar extends Foo, Bar { FooBarReturn fooBar(); } Javac失败并显示以下消息: FooBar.java:2: types Bar and Foo are incompatible; both define fooBar(), but with unrelated […]

为什么Java 5+ API不利用协变返回类型?

从Java 5开始,我们可以使用协变返回类型 。 为什么Java API不利用这个? 以Graphics2D.create()为例。 为什么不重写返回Graphics2D对象? 在我看来,它在所有情况下都是向后兼容的。