【发布时间】:2015-04-23 05:20:54
【问题描述】:
我有一个接口,该接口带有一个返回有界通配符的方法:
public Collection<? extends CacheableObject> retrieveConnections(CacheableObject element);
这个接口是由一个类名PersistentAbstraction实现的,它返回一个CacheableObject:
public Collection<? extends CacheableObject> retrieveConnections(CacheableObject element) {
Collection<CacheableObject> connections = new HashSet<CacheableObject>();
for(CacheableRelation relation : connectedElements) {
if(relation.contains(element)) {
connections.add(relation.getRelatedElement(element));
}
}
return connections;
}
现在我有一个名为User 的CacheableObject 实现和一个具有PersistentAbstraction 实例的类。我正在尝试执行以下操作:
public Collection<User> retrieveConnections(User user) {
Collection<User> collection = (Collection<User>) persistentAbstraction.retrieveConnections(user);
return collection;
}
但它说:
Type safety: Unchecked cast from Collection<capture#1-of ? extends CacheableObject> to Collection<User>
我不确定该警告背后的原因是什么。 User 不是 CacheableObject 吗?警告是什么意思?
【问题讨论】: