Rin*_*nke 2 java generics casting wrapper
请考虑以下界面.
public interface Feed<T> {
public void put( T o );
}
Run Code Online (Sandbox Code Playgroud)
我们有一个简单的Java类来实现这个接口,它将对象写入特定的集合.
public class CollectionFiller<T> implements Feed<T> {
private final Collection<T> collection;
private CollectionFiller( Collection<T> collection ) {
this.collection = collection;
}
public void put( T o ) {
this.collection.add( o );
}
public static <T> CollectionFiller<T> of( Collection<T> collection ) {
return new CollectionFiller<T>( collection );
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们定义两个虚拟类,以使问题具体化.
public class Super {}
public class Sub extends Super {}
Run Code Online (Sandbox Code Playgroud)
比方说,有一些方法writeSubsTo( Feed<Sub> f ),我们有一个实例cf的CollectionFiller<Super>.由于cf只将对象添加到其"Supers"集合中,因此将其传递给它是安全的writeSubsTo.但编译器不允许这样做.(这种行为的原因对我来说很清楚.)
我想编写一个方便的适配器,它包装了一个CollectionFillerX类型并构成一个Feed类型为X的特定子类型.我尝试了(以及其他许多东西)以下内容,但编译器给我带来了麻烦.
class SubFiller<T1, T2 extends T1> implements Feed<T2> {
private final CollectionFiller<T1> collectionFiller;
SubFiller( CollectionFiller<T1> collectionFiller ) {
this.collectionFiller = collectionFiller;
}
public void put( T2 o ) {
this.collectionFiller.put( o );
}
public static <T1, T2 extends T1> SubFiller<T1, T2> of( CollectionFiller<T1> c ) {
return new SubFiller<T1, T2>( c );
}
}
Run Code Online (Sandbox Code Playgroud)
虽然此类没有任何问题,但编译器不会接受以下代码片段中的最后两个语句.
CollectionFiller<Super> cf = CollectionFiller.of( new HashSet<Super>() );
SubFiller<Super, Sub> sf = SubFiller.of( cf );
writeSubsTo( SubFiller.of( cf ) );
Run Code Online (Sandbox Code Playgroud)
谁能想到解决这个问题的好方法?我不介意适配器是否包含繁琐的代码,只要使用它不是太冗长(因此静态工厂方法).当然,任何其他解决方案(不使用适配器)也很好(再次,只要使用它不是太冗长).
如果你使用writeSubsTo(Feed<? super Sub> f),那么你可以传入Feed<Super>.然后你可以完全取消你的填充类.是的逆转!:-P
(还有一个协变版本,? extends X适用于你获得价值而不是投入价值的情况.)