Java-泛型澄清
我有以下代码:Java-泛型澄清,java,generics,Java,Generics,我有以下代码: interface Id{ int getA(); } class Id1 implements Id{ @Override public int getA() { return 0; } } class Id2 implements Id{ @Override public int getA() { return 0; } public int getB(){ ret
interface Id{
int getA();
}
class Id1 implements Id{
@Override
public int getA() {
return 0;
}
}
class Id2 implements Id{
@Override
public int getA() {
return 0;
}
public int getB(){
return 2;
}
}
interface Container<I extends Id, C extends Container<I,C>>{
C withInt(int a);
}
class DefaultContainer<I extends Id> implements Container<I, DefaultContainer<I>>{
@Override
public DefaultContainer<I> withInt(int a) {
return new DefaultContainer<>();
}
}
class SpecialContainer<I extends Id> extends DefaultContainer<I>{
@Override
public SpecialContainer<I> withInt(int a) {
return new SpecialContainer<>();
}
}
interface ContainerGroup<C extends Container<? extends Id, C>>{
Stream<C> getContainers();
}
class DefaultContainerGroup<DC extends DefaultContainer<? extends Id>>
implements ContainerGroup<DC>{
//Error:(36, 35) java: type argument DC is not within bounds of type-variable C
@Override
public Stream<DC> getContainers() {
//can return:
// [ DefaultContainer<Id>, DefaultContainer<Id2>, ... ] OR
// [ SpecialContainer<Id>, SpecialContainer<Id2>, ... ]
return null;
}
}
接口Id{
int getA();
}
类Id1实现了Id{
@凌驾
公共int getA(){
返回0;
}
}
类Id2实现了Id{
@凌驾
公共int getA(){
返回0;
}
公共int getB(){
返回2;
}
}
接口容器{
C带int(int a);
}
类DefaultContainer实现容器{
@凌驾
公共默认容器withInt(int a){
返回新的DefaultContainer();
}
}
类SpecialContainer扩展了DefaultContainer{
@凌驾
带int的公共特殊容器(INTA){
返回新的特殊容器();
}
}
接口ContainerGroup我想我知道发生了什么
在DefaultContainerGroup
中,DC扩展DefaultContainer
interface ContainerGroup<C extends Container<? extends Id, ? extends Container>>{
Stream<C> getContainers();
}