在下面的代码片段中,当我尝试将 Class>B> 转换为 Class>A> 时,为什么 1 不会产生运行时异常?
package example;
Class A {
public A() {
}
}
Class B extends A {
public B() {
}
}
public static void main() {
Class<A> c = null;
//1. Does not produce exception at run-time even though I cast Class<B> to Class<A>
try {
c = (Class<A>) Class.forName("example.B");
} catch (ClassNotFoundException e) {
}
//2. Compile time error: Cannot Cast Class<B> to Class<A>
c = (Class<A>) B.class; //Error
}