jav*_*ent 7 java generics classcastexception
我正在阅读Java Generics,我遇到了这个主题,我有点困惑.
来自: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);
}
}
Run Code Online (Sandbox Code Playgroud)
滚动降低几个屏幕......
public abstract class Node <N extends Node<N>> {
...
protected Node(N parent) {
this.parent = parent;
parent.children.add( (N)this ); // warning: unchecked cast
}
...
}
Run Code Online (Sandbox Code Playgroud)
目标类型为类型参数的强制转换无法在运行时验证,并导致未经检查的警告.这种不安全的演员引入了意外的ClassCastException的可能性,最好避免使用.
有人能给我一个例子,上面的代码抛出ClassCastException吗?
谢谢.
在第一个代码示例中,存在编译错误.您可以在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的不同子类,因此添加它可能是错误的.
现在第二个代码示例是开发人员,他厌倦了他不理解的编译时错误,认为编译器是错误的并试图强制转换.这样的强制转换会使代码编译,但可能会在相同条件下在运行时中断(如上所述).
因此,编译器会发出警告,以帮助您了解某些内容可能出错.
对于这两个以前的代码示例,如果调用代码写入(对于两个子类可能发生的问题NodeA
和NodeB
的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);
Run Code Online (Sandbox Code Playgroud)
在第二行,将在构造函数中运行的代码Node
将解释为(用N
当前参数替换format 参数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
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
如您所见,调用者的第二行将传递一个NodeA
实例,而构造函数则Node
需要一个NodeB
!因此错误......
注释要求的更新:子类NodeA(或NodeB)的示例代码.
public class NodeA extends Node<NodeA> {
public NodeA(NodeA parent) {
super(parent);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1759 次 |
最近记录: |