We have the following organization of classes.
class Parent { }
class DerivedOne extends Parent { }
class DerivedTwo extends Parent { }
Which of the following statements is correct for the following expression?
Parent p = new Parent();
DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)d2;
Illegal both compile and runtime
Legal at compile time, but fails at runtime
Legal at compile and runtime
None of the above
给出的答案是A,他的解释如下:
解释是
A is correct. You cannot assign an object to a sibling(no parent-child relation) reference, even with casting.
还请高手指教下。。。。
[此贴子已经被作者于2006-8-11 17:11:24编辑过]
----------------解决方案--------------------------------------------------------
We have the following organization of classes.
class Parent { }
class DerivedOne extends Parent { }
class DerivedTwo extends Parent { }
Which of the following statements is correct for the following expression?
Parent p = new Parent();
DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)d2;
Illegal both compile and runtime
Legal at compile time, but fails at runtime
Legal at compile and runtime
None of the above
给出的答案是A,他的解释如下:
解释是
A is correct. You cannot assign an object to a sibling(no parent-child relation) reference, even with casting.
还请高手指教下。。。。
父类是动物,子类分别为猫和狗,不能把猫转换成狗,狗也不能转换为猫
----------------解决方案--------------------------------------------------------
哦 明白了 谢
----------------解决方案--------------------------------------------------------