可以说Class B
扩展class A
并且class A
是可克隆的,如下所示:
public class A implements Cloneable {
public Object clone() throws CloneNotSupportedException {
A ac = (A) super.clone();
return ac;
}
}
public class B extends A {
public Object clone() throws CloneNotSupportedException {
B a = (B) super.clone();
return a;
}
}
为什么在下一行从 A 向下转换到 B 是合法的:
B a = (B) super.clone(); // (super of B is A)
而下一个向下转换是运行时错误?
A a = new A();
B b = (B) a.clone();
提前致谢!