简体   繁体   English

Java泛型扩展列表

[英]Java Generics extending List

I have the following classes: 我有以下课程:

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>> {

}

Then I use it as: 然后我将其用作:

What what = new What<MyFirstClass>();

When I want to use MyFirstClass as a type parameter which extend BaseListResponse<ServiceSynchronizableEntity>> it shows me 当我想使用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

What is wrong? 怎么了?

Edit: One class was missing. 编辑:缺少一堂课。 Look at: http://ideone.com/D4snKP 查看: http : //ideone.com/D4snKP

Thanks! 谢谢!

The solution I previously posted (now deleted) was similar in that it required a change to the generic signature. 我以前发布(现在已删除)的解决方案类似,因为它需要更改通用签名。

The important change is going from: 重要的变化来自:

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

to: 至:

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

See http://ideone.com/wyXvcl for the fixed version 参见http://ideone.com/wyXvcl以获取固定版本

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

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