从超类到子类的显式转换
2022-08-31 06:49:59
public class Animal {
public void eat() {}
}
public class Dog extends Animal {
public void eat() {}
public void main(String[] args) {
Animal animal = new Animal();
Dog dog = (Dog) animal;
}
}
该赋值不会生成编译错误,但在运行时会生成一个 .为什么编译器无法检测到此错误?Dog dog = (Dog) animal;
ClassCastException