我遇到了一个包含使用泛型成员的类的麻烦。考虑以下示例类:

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 实际上不是StructureImpl 的子类。因此,我尝试修改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子类的结构的要求,同时又能够使用列表中的元素调用getDependencies?

最佳答案

修改您的最新版本以捕获通配符。

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
    }
}

09-27 00:01