2012-02-08 60 views
0

我有以下构造函数的类:转换集合<MyType>到收藏<Object>

public UniqueField(Collection<Object> items) { 
     this.items=items; 
} 

Collection<Object>背后的想法是,我将能够使用Collection<OtherType>

在做:

Collection<OtherType> collection=... 
new UniqueField(collection); 

我越来越无效参数的编译错误。我怎样才能解决这个问题?

回答

4

你必须用这个代替

public UniqueField(Collection<? extends Object> items) { 
     this.items=items; 
} 

或?因为它等于 “?扩展对象”

public UniqueField(Collection<?> items) { 
     this.items=items; 
} 

你可以看到here的原因

0

您可以使用:

public UniqueField(Collection<?> items) { 
     this.items=items; 
} 

或:

public UniqueField(Collection<? super OtherType> items) { 
     this.items=items; 
} 

或简单地说:

public UniqueField(Collection<OtherType> items) { 
    this.items=items; 
} 
相关问题