【发布时间】:2021-09-29 08:40:27
【问题描述】:
请先看看我的代码。当我想创建一个对象时,我在 BolBFS 构造函数中遇到错误:
Error:
Required type is : Consumer<Node<String, ChildFlightConsignmentInfo>>
Provided type is : Consumer<BolNode>
我知道我不能将consumer<T> 转换为consumer\<? extends T>,但我不知道为什么,我应该如何处理这个问题?
如果我将 Node
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;
}
}
节点类:
public abstract class Node<Identifier, Data> implements Serializable {
public abstract Set<Node<Identifier, Data>> getChild();
}
【问题讨论】:
-
我认为
? extends T的意思是“某物,但从 T 延伸的非常具体的东西”。 A,B,C... 可以匹配 T 如果所有扩展 T,并且它们都可以完全是 T。但是如果你将它转换为?那么它是哪一个可能的匹配? -
为什么你的很多字段都是
volatile? -
为什么要声明
N extends Node<Identifier, Data>,然后在构造函数参数类型中使用Node<Identifier, Data>? -
@Antoniossss 我说
? extends T发送我的概念,我的意思是从T 延伸,在我的例子中? = BolNode和T = Node。 -
不,T 是 T,BolNode 是 BolNode。 ?是“某事”,即使您当前的情况可以扣除它会是什么。