我有一个简单的接口:
public interface Node<E extends Node<E>>
{
public E getParent();
public List<E> getChildren();
default List<E> listNodes()
{
List<E> result = new ArrayList<>();
// ------> is this always safe? <-----
@SuppressWarnings("unchecked")
E root = (E) this;
Queue<E> queue = new ArrayDeque<>();
queue.add(root);
while(!queue.isEmpty())
{
E node = queue.remove();
result.add(node);
queue.addAll(node.getChildren());
}
return result;
}
}
我看到this
总是Node<E>
的一个实例(根据定义(
但我无法想象this
不是E
的实例的情况
既然E extends Node<E>
,那么Node<E>
在定义上不应该也等同于E
吗
你能举一个对象的例子,它是Node<E>
的实例,但不是E
的实例吗??
与此同时,我的大脑正在融化。。。
前面的类是一个简化的例子
为了显示为什么我需要一个自绑定,我添加了一点复杂性:
public interface Node<E extends Node<E, R>, R extends NodeRelation<E>>
{
public List<R> getParents();
public List<R> getChildren();
default List<E> listDescendants()
{
List<E> result = new ArrayList<>();
@SuppressWarnings("unchecked")
E root = (E) this;
Queue<E> queue = new ArrayDeque<>();
queue.add(root);
while(!queue.isEmpty())
{
E node = queue.remove();
result.add(node);
node.getChildren()
.stream()
.map(NodeRelation::getChild)
.forEach(queue::add);
}
return result;
}
}
public interface NodeRelation<E>
{
public E getParent();
public E getChild();
}
一个简单的例子来说明这个问题:不同类型节点的节点:
class NodeA implements Node<NodeA> {
...
}
和:
class NodeB implements Node<NodeA> {
...
}
在这种情况下,E root = (E) this
将解析为NodeA root = (NodeA) this
,其中this
是NodeB
。这是不相容的。
如果没有<E extends Node<E>>
,您可能会出现以下任一情况:
Node<Integer>
其中泛型类型根本不是Node
或
Node<DifferentNode>
其中泛型边界不匹配。
也就是说,以这种方式看到绑定是而不是的典型情况,因为Node<E>
被期望为包含某种类型E
的值的节点,并且children
将是List<Node<E>>
,而不是List<E>
。
问题不在E root = (E) this
中。在您开始迭代listNodes()
的结果之前,它可能工作得很好。
该示例演示了ClassCastException
将被抛出的确切位置:
public interface Node<E extends Node<E>> {
List<E> getRelatedNodes();
default List<E> getAllNodes() {
List<E> result = new ArrayList<>();
result.add((E) this); //<--that cast is not a problem because of type erasure
return result;
}
}
class NodeA implements Node<NodeA> {
public NodeA() {
}
@Override
public List<NodeA> getRelatedNodes() {
return null;
}
}
class NodeB implements Node<NodeA> {
private List<NodeA> relatedNodes;
public NodeB(List<NodeA> relatedNodes) {
this.relatedNodes = relatedNodes;
}
@Override
public List<NodeA> getRelatedNodes() {
return relatedNodes;
}
}
执行:
List<NodeA> nodes = new NodeB(Arrays.asList(new NodeA())).getAllNodes(); //according to generic it is list of NodeA objects
for (NodeA node : nodes) { //ClassCastException will be thrown
System.out.println(node);
}
在这种情况下,有一个getThis
方法(按惯例(返回this
通常很有用。
我会做以下
public interface Node<E extends Node<E, R>,
R extends NodeRelation<E, R>>
{
public List<R> getParents();
public List<R> getChildren();
public List<E> listDescendants() ;
}
public interface NodeRelation<E extends Node<E, R>,
R extends NodeRelation<E, R>>
{
public E getParent();
public E getChild();
}
abstract class ANode<E extends ANode<E,R>,
R extends ARelation<E,R>>
implements Node<E,R> {
abstract protected E getThis() ;
public List<E> listDescendants()
{
List<E> result = new ArrayList<>();
E root = getThis() ;
...
return result;
}
}
abstract class ARelation<E extends ANode<E,R>,
R extends ARelation<E,R>>
implements NodeRelation<E,R> {
}
class CNode extends ANode<CNode, CRelation> {
public CNode getThis() { return this ; }
...
}
class CRelation extends ARelation<CNode, CRelation> {
...
}
尽管我可能不会同时拥有抽象类和接口层。