繁体   English   中英

Java泛型扩展列表

[英]Java Generics extending List

我有以下课程:

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

谢谢!

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

重要的变化来自:

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

至:

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

参见http://ideone.com/wyXvcl以获取固定版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM