当我有这些代码时:
ASuperClass super1 = new ASuperClass();
ASubClass sub1 = new ASubClass(3);
sub1 = (ASubClass) super1; // this line compiled ok BUT has runtime Error LINE 3
ASubClass sub2 = new ASuperClass(); // this line compiled NOT ok LINE 4
我的问题是,为什么第 3 行中的错误(“ASuperClass cannot be cast to ASubClass”)在第 3 行是运行时错误,而不是类似于第 4 行中的错误的编译错误,这是一个编译错误。这背后的逻辑是什么?非常感谢!