Given:
11. class Animal{public String noise(){return "peep";}}
12. class Dog extends Animal{
13. public String noise(){return "bark";}
14 }
15. class Cat extends Animal{
16. public String noise(){return "meow";}
17. }
...
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.println(cat.noise());
What is the result?
A. peep
B. bark
C. meow
D. Compilation fails.
E. An exception is thrown at runtime.
答案:E
參考:7-8 物件的轉型
---------------------------------------
animal實體為dog, animal不能轉型為貓
沒有留言:
張貼留言