Java Generics扩展List

时间:2014-10-14 19:40:55

标签: java generics

我有以下课程:

public interface ServiceSynchronizableEntity {
    ....
}

public class BaseListResponse<T> {
    ....
}

public class BaseSynchronizableListResponse<T extends ServiceSynchronizableEntity> extends BaseListResponse<T> {
    ....
}

public class MySecondClass implements ServiceSynchronizableEntity {
    ....
}

public class MyFirstClass extends BaseSynchronizableListResponse<MySecondClass> {
    ....
}

public class What<S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity>> {

}

然后我用它作为:

What what = new What<MyFirstClass>();

当我想使用MyFirstClass作为扩展BaseListResponse<ServiceSynchronizableEntity>>的类型参数时,它会显示我

Main.java:26: error: type argument MyFirstClass is not within bounds of type-variable S
    What what = new What<MyFirstClass>();
                         ^
where S is a type-variable: S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity> declared in class What

有什么问题?

编辑:缺少一个班级。请看:http://ideone.com/D4snKP

谢谢!

1 个答案:

答案 0 :(得分:1)

我之前发布的解决方案(现已删除)类似,因为它需要更改通用签名。

重要的变化来自:

class What<S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity>> {}

为:

class What<S extends BaseSynchronizableListResponse<? extends ServiceSynchronizableEntity>> {}

有关固定版本

,请参阅http://ideone.com/wyXvcl