7

我正在阅读有关 Java 泛型的信息,并且遇到了这个让我有点困惑的话题。

来自:http ://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205

public abstract class Node <N extends Node<N>>  {
   private final List<N> children = new ArrayList<N>();
   private final N parent;

   protected Node(N parent) {
     this.parent = parent;
     parent.children.add(this);  // error: incompatible types
   }
   public N getParent() {
     return parent;
   }
   public List<N> getChildren() {
     return children;
   }
 }

public class SpecialNode extends Node<SpecialNode> {
   public SpecialNode(SpecialNode parent) {
     super(parent);
   }
} 

向下滚动几个屏幕...

public abstract class Node <N extends Node<N>>  {
   ...
   protected Node(N parent) {
     this.parent = parent;
     parent.children.add( (N)this ); // warning: unchecked cast
   }
   ...
 }

目标类型为类型参数的强制转换无法在运行时验证并导致未经检查的警告。这种不安全的强制转换引入了意外 ClassCastException 的可能性,最好避免。

有人可以给我一个上面的代码抛出 ClassCastException 的例子吗?

谢谢。

4

1 回答 1

5

第一个代码示例

在第一个代码示例中,存在编译错误。您可以在 IDE 中自行验证。

我的说:The method add(N) in the type List<N> is not applicable for the arguments (Node<N>)

问题是 N 是 Node 的子类型。N 的列表可能是 StupidNode 的列表,其中 StupidNode 是 Node 的子类。但是当前实例可能不是 StupidNode,它可能是 Node 的不同子类,因此添加它可能是错误的。


第二个代码示例

现在第二个代码示例是开发人员对他不理解的编译时错误感到恼火,认为编译器错误并尝试强制转换的代码示例。这样的强制转换使代码编译,但可能在相同条件下在运行时中断(如上文所述)。

因此,编译器会发出警告,以帮助您了解可能有问题。


示例问题

对于前面的两个代码示例,如果调用代码写入(对于两个子类NodeANodeBof Node),则可能会发生问题:

Node<NodeA> root = new NodeA<NodeA>(null); 
// code needs a change, to be able to handle the root, that has no parent
// The line with parent.children will crash with a NullPointerException
Node<NodeB> child = new NodeB<NodeB>(root);

在第二行,将在 的构造函数中运行的代码Node将解释为(将格式参数替换N为当前参数NodeB):

public abstract class Node <NodeB>  {
   private final List<NodeB> children = new ArrayList<NodeB>();
   private final NodeB parent;

   protected Node(NodeB parent) {
     this.parent = parent;
     parent.children.add(this);  // error: incompatible types
   }
  // ...
 }

如您所见,调用者的第二行将传递一个NodeA实例,而构造函数Node需要一个NodeB!因此错误...


按照评论的要求更新:子类 NodeA(或 NodeB)的示例代码。

public class NodeA extends Node<NodeA>  {
   public NodeA(NodeA parent) {
     super(parent);
   }
}
于 2009-12-24T13:12:14.730 回答