我在理解 K&B 的 SCJP 书中的面向对象章节中的第 9 题时遇到问题。
问题:
public class Redwood extends Tree {
public static void main (String [] args) {
new Redwood ( ) . go ( ) ;
}
void go ( ) {
go2 (new Tree ( ) , new Redwood ( ) ) ;
go2 ( (Redwood) new Tree ( ) , new Redwood ( ]
}
void go2 (Tree tl, Redwood rl) {
Redwood r2 = (Redwood) tl;
Tree t2 = (Tree)rl;
}
}
class Tree { }
选项:
What is the result? (Choose all that apply.)
A. An exception is thrown at runtime
B. The code compiles and runs with no output
C. Compilation fails with an error at line 8
D. Compilation fails with an error at line 9
E. Compilation fails with an error at line 12
F. Compilation fails with an error at line 13
书中给出的答案是 A,因为 Tree 不能落入 Redwood。我只是在理解这个概念时遇到了问题。