"SCJP 1.6版考題 062" 修訂間的差異
出自 陳富國維基館
(新頁面: 31. class Foo{ 32. public int a = 3; 33. public void addFive(){ a += 5; System.out.print("f "); } 34. } 35. class Bar extends Foo{ 36. public int a = 8; 37. public void ...) |
(無差異)
|
於 2013年3月30日 (六) 07:00 的最新修訂
31. class Foo{
32. public int a = 3;
33. public void addFive(){ a += 5; System.out.print("f "); }
34. }
35. class Bar extends Foo{
36. public int a = 8;
37. public void addFive(){this.a += 5; System.out.print("b ");}
38. }
Invoked with:
Foo f = new Bar();
f.addFive();
System.out.println(f. a);
What is the result?
A. b 3
B. b 8
C. b 13
D. f 3
E. f 8
F. f 13
G. Compilation fails,
H. An exception is thrown at runtime.
解答
Ans: A
解說:
方法的多形是以物件的型態叫用適當的方法 addFive是物件成員 f物件參考雖是Foo型態,但是指向Bar物件,因此f.addFive是叫用Bar物件型態中的addFive方法,所以是印出b 而f.a是以f的型態取出Foo物件型態中的a(沒有變數的多形這種說法)
| ||||||||||||||||||||