SCJP題庫第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 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(沒有變數的多形這種說法)
Comments