Questions tagged «classcastexception»

7
从父类到子类的显式转换
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。为何编译器无法检测到此错误?


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.