我在使用包含使用泛型的成员的类时遇到了一些问题。请考虑以下示例类:
class BaseRequest {
// Content goes here
}
class SubRequest extends BaseRequest {
// IFace methods go here, etc.
}
// Simple datastructure interface
interface Structure<T> {
List<T> getElementsList();
Collection<T> getDependencies(T t);
}
// Simple structure implementation, details unimportant
class StructureImpl<T> implements Structure<T> {
@Override
public List<T> getElementsList() {
// Implementation details not important
return null;
}
@Override
public Collection<T> getDependencies(T t) {
// implementation details not important
return null;
}
}
然后我有以下使用结构
的类class StructureUsingObject {
final Structure<BaseRequest> structure;
public StructureUsingObject(Structure<BaseRequest> structure) {
this.structure = structure;
}
public void foo() {
for (BaseRequest request : structure.getElementsList()) {
Collection<BaseRequest> requests = this.structure.getDependencies(request);
// do things
}
}
}
本身,这不起作用;
public void foo() {
StructureImpl<BaseRequest> baseImpl = new StructureImpl<BaseRequest>();
StructureUsingObject baseStruct = new StructureUsingObject(baseImpl);
StructureImpl<SubRequest> subImpl = new StructureImpl<SubRequest>();
// Compile error on this line
StructureUsingObject subStruct = new StructureUsingObject(subImpl);
}
这是有道理的; StructureImpl&LT; BaseRequest&GT;不是StructImpl&lt; SubRequest&gt;的子类。因此,我尝试修改StructureUsingObject以使用通配符:
class StructureUsingObject {
final Structure<? extends BaseRequest> structure;
public StructureUsingObject(Structure<? extends BaseRequest> structure) {
this.structure = structure;
}
public void foo() {
for (BaseRequest request : structure.getElementsList()) {
// Compile error on this line
Collection<BaseRequest> requests = this.structure.getDependencies(request);
// To clarify, a compile error would happen here as well; the issue isn't with the return type
this.structure.getDependencies(request);
// do things
}
}
}
这也不起作用;
The method getDependencies(capture#4-of ? extends BaseRequest) in the type Structure<capture#4-of ? extends BaseRequest> is not applicable for the arguments (BaseRequest)
好的,这也是有道理的,虽然自动完成工具提示没有(它声明getDependencies应该有(null t)作为其参数)。我不确定从哪里开始。我不能在foreach中使用通配符。如何满足我的StructureUsingObject可以接受具有BaseRequest子类的Structures的要求,同时还能够使用列表中的元素调用getDependencies?
答案 0 :(得分:2)
调整您的上一个版本以捕获通配符。
public void foo() {
bar(this.structure);
}
private <T extends BaseRequest> void bar(Structure<T> s) {
for (T request : s.getElementsList()) {
Collection<T> requests = s.getDependencies(request);
// do things
}
}