当前位置: 代码迷 >> Java相关 >> [求助]有答案都不明白什么意思。。。
  详细解决方案

[求助]有答案都不明白什么意思。。。

热度:165   发布时间:2006-08-11 17:08:04.0
[求助]有答案都不明白什么意思。。。

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编辑过]


----------------解决方案--------------------------------------------------------
以下是引用wxhwjsw在2006-8-11 17:08:04的发言:

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.
还请高手指教下。。。。


父类是动物,子类分别为猫和狗,不能把猫转换成狗,狗也不能转换为猫
----------------解决方案--------------------------------------------------------
哦  明白了 谢  
----------------解决方案--------------------------------------------------------

  相关解决方案