Java 泛型:某种类型的超级的集合
Java generics: collection of super of super of some type
考虑以下自包含示例:
import java.util.*;
class TestApplication
{
interface Type<C>
{
Collection<Type<? super C>> getSuperTypes();
}
static class Test<C>
{
private final Type<? super C> mySuperType = get( null );
public Collection<Type<? super C>> getSuperTypes()
{
Collection<Type<? super C>> superTypes = new ArrayList<>();
superTypes.add( mySuperType );
//directly passing the super-types to addAll() works
superTypes.addAll( mySuperType.getSuperTypes() );
//but how can I declare the variable to temporarily hold the supers?
Collection<Type<? super C>> superSuperTypes = mySuperType.getSuperTypes(); //ERROR
superTypes.addAll( superSuperTypes );
return superTypes;
}
}
public static <T> T get( T value )
{
return value;
}
}
所以,我有这个 class 代表一个类型,它有一个超级类型,而超级类型又有超级类型,我想要一个函数 returns 一个平面一个类型的所有超类型的集合。
因此,我声明了一个超类型集合,并向其中添加了当前类型的直接超类型,然后我还需要添加超类型的超类型。 (不要介意它的效率低得可怕,实际上我以一种更有效的方式来做,但这无关紧要。)
因此,目标是调用 superTypes.addAll()
并将 superType.getSuperTypes()
的结果传递给它。
当superType.getSuperTypes()
的结果直接传给superTypes.addAll()
,没有中间变量,没有问题。
但是如果我想在将 superType.getSuperTypes()
的结果传递给 superTypes.addAll()
之前声明一个中间变量 superSuperTypes
来保存它,我找不到一种方法来声明该变量以便它会编译。就目前而言,它给出以下消息:
Error:(100, 84) java: incompatible types: java.util.Collection<TestApplication.Type<? super capture#1 of ? super C>> cannot be converted to java.util.Collection<TestApplication.Type<? super C>>
那么:应该如何声明superSuperTypes
才能将superType.getSuperTypes()
的结果赋值给它然后传递给superTypes.addAll()
?
喜欢
Collection<? extends Type<? super C>> superSuperTypes = mySuperType.getSuperTypes();
考虑以下自包含示例:
import java.util.*;
class TestApplication
{
interface Type<C>
{
Collection<Type<? super C>> getSuperTypes();
}
static class Test<C>
{
private final Type<? super C> mySuperType = get( null );
public Collection<Type<? super C>> getSuperTypes()
{
Collection<Type<? super C>> superTypes = new ArrayList<>();
superTypes.add( mySuperType );
//directly passing the super-types to addAll() works
superTypes.addAll( mySuperType.getSuperTypes() );
//but how can I declare the variable to temporarily hold the supers?
Collection<Type<? super C>> superSuperTypes = mySuperType.getSuperTypes(); //ERROR
superTypes.addAll( superSuperTypes );
return superTypes;
}
}
public static <T> T get( T value )
{
return value;
}
}
所以,我有这个 class 代表一个类型,它有一个超级类型,而超级类型又有超级类型,我想要一个函数 returns 一个平面一个类型的所有超类型的集合。
因此,我声明了一个超类型集合,并向其中添加了当前类型的直接超类型,然后我还需要添加超类型的超类型。 (不要介意它的效率低得可怕,实际上我以一种更有效的方式来做,但这无关紧要。)
因此,目标是调用 superTypes.addAll()
并将 superType.getSuperTypes()
的结果传递给它。
当superType.getSuperTypes()
的结果直接传给superTypes.addAll()
,没有中间变量,没有问题。
但是如果我想在将 superType.getSuperTypes()
的结果传递给 superTypes.addAll()
之前声明一个中间变量 superSuperTypes
来保存它,我找不到一种方法来声明该变量以便它会编译。就目前而言,它给出以下消息:
Error:(100, 84) java: incompatible types: java.util.Collection<TestApplication.Type<? super capture#1 of ? super C>> cannot be converted to java.util.Collection<TestApplication.Type<? super C>>
那么:应该如何声明superSuperTypes
才能将superType.getSuperTypes()
的结果赋值给它然后传递给superTypes.addAll()
?
喜欢
Collection<? extends Type<? super C>> superSuperTypes = mySuperType.getSuperTypes();