关于类型擦除的 Java教程似乎没有详细说明编译器强制转换插入的具体规则。有人可以解释导致教程详述的转换的具体规则(转载如下):
public class Node<T> {
public T data;
public Node(T data) { this.data = data; }
public void setData(T data) {
System.out.println("Node.setData");
this.data = data;
}
}
public class MyNode extends Node<Integer> {
public MyNode(Integer data) { super(data); }
public void setData(Integer data) {
System.out.println("MyNode.setData");
super.setData(data);
}
}
MyNode mn = new MyNode(5);
Node n = (MyNode)mn; // A raw type - compiler throws an unchecked warning
n.setData("Hello");
Integer x = (String)mn.data; // Causes a ClassCastException to be thrown.
具体来说,我想知道是什么规则导致了(MyNode)
and的插入(String)
。什么时候插入演员表,演员表的类型是如何选择的?