将消费者泛型类型转换为另一种类型

Casting consumer generic type to another type

请先看我的代码。当我想创建一个对象时,我在 BolBFS 构造函数中遇到错误:

Error:
Required type is : Consumer<Node<String, ChildFlightConsignmentInfo>>
Provided type is : Consumer<BolNode>

我知道我不能将 consumer<T> 转换为 consumer\<? extends T>,但我不知道为什么,我应该如何处理这个问题?
如果我将 Node 更改为 N,我在 adj() 方法中遇到另一个问题 class BFS.

Error:
Required Set<N>
Provided Provided Set<Node<Identifier, Data>>

这是我的代码:

BolBFS.java构造函数:

public BolBFS(TraverseDirection dir, BolNode root, Consumer<BolNode> consumeEachBol, Predicate<BolNode> setFlagCondition, Predicate<BolNode> stopCondition) {
    BFS<String, ChildFlightConsignmentInfo, BolNode> bfs =  new BFS<>(dir, root, consumeEachBol, setFlagCondition, stopCondition);
}

BolNode.java:

public class BolNode extends Node<String, ChildFlightConsignmentInfo>

BFS.java:

public class BFS<Identifier, Data, N extends Node<Identifier, Data>> implements Serializable {

    protected TraverseDirection dir;
    protected volatile Queue<Node<Identifier, Data>> nextToVisit;
    protected volatile boolean flag;
    protected volatile Set<Object> visited;
    protected Consumer<Node<Identifier, Data>> consumer;
    protected Predicate<Node<Identifier, Data>> checker;
    protected Predicate<Node<Identifier, Data>> stopCondition;
    protected volatile Map<Object, Node<Identifier, Data>> predecessor;
    protected volatile Map<Integer, Set<Node<Identifier, Data>>> levels;
    protected volatile Map<Object, Set<Object>> adjList;
    protected volatile Map<Object, Node<Identifier, Data>> idMap;

    public BFS(TraverseDirection dir, Node<Identifier, Data> root, Consumer<Node<Identifier, Data>> consumer, Predicate<Node<Identifier, Data>> checker, Predicate<Node<Identifier, Data>> stopCondition) {        this.dir = dir;
        this.flag = false;
        this.checker = checker;
        this.consumer = consumer;
        this.stopCondition = stopCondition;
        this.predecessor = new ConcurrentHashMap<>();
        this.levels = new ConcurrentHashMap<>();
        this.adjList = new ConcurrentHashMap<>();
        this.idMap = new ConcurrentHashMap<>();
        idMap.put(root.getId(), root);
        levels.put(0, Stream.of(root).collect(Collectors.toSet()));
        logic(root, consumer, checker, stopCondition);
    }
    protected Map<TraverseDirection, Set<N>> adj(N node) {
        Map<TraverseDirection, Set<N>> result = new ConcurrentHashMap<>();
        result.put(TraverseDirection.ANY, node.getChild());
        return result;
    }

}

Node.class:

public abstract class Node<Identifier, Data> implements Serializable {
    public abstract Set<Node<Identifier, Data>> getChild();
    }

问题是 Consumer<BolNode> 不是 Consumer<Node<String, ChildFlightConsignmentInfo>>,原因与 a List<Dog> is not a List<Animal> 相同。更具体地说,Consumer<BolNode> 期望它总是传递给 BolNode,而 Consumer<Node<String, ChildFlightConsignmentInfo>> 可能传递给 Node<String, ChildFlightConsignmentInfo>.[=22 的任何子 class =]

您已在 BFS class 中声明了类型参数 N extends Node<Identifier, Data>,但随后未在构造函数参数类型中使用它。我建议尝试:

public BFS(
    TraverseDirection dir,
    N root,
    Consumer<N> consumer,  // Or Consumer<? super N>
    Predicate<N> checker,  // Or Predicate<? super N>
    Predicate<N> stopCondition) {