不幸的是,这不能由Java的类型系统强制执行。
但是,您可以通过使用以下方法来达到非常接近的效果:
public interface Bar<T extends Bar<T>> { List<T> getFoo();}然后您的实现类可以像这样实现它:
public class SomeSpecificBar implements Bar<SomeSpecificBar> { // Compiler will enforce the type here @Override public List<SomeSpecificBar> getFoo() { // ... }}但是没有什么可以阻止另一个类这样做:
public class EvilBar implements Bar<SomeSpecificBar> { // The compiler's perfectly OK with this @Override public List<SomeSpecificBar> getFoo() { // ... }}


