SCJP 1.6版考題 066

出自 陳富國維基館
前往: 導覽搜尋
5. class Thingy{ Meter m = new Meter(); }
6. class Component {void go() { System.out.print("c");}}
7. class Meter extends Component {void go() { System.out.print("m"); }}
8.
9. class DeluxeThingy extends Thingy {
10.   public static void main(String[] args) {
11.     DeluxeThingy dt = new DeluxeThingy();
12.     dt.m.go();
13.     Thingy t = new DeluxeThingy();
14.     t.m.go();
15.   }
16. }


Which two are true? (Choose two.)
  A. The output is mm.
  B. The output is mc.
  C. Component is-a Meter.
  D. Component has-a Meter.
  E. DeluxeThingy is-a Component.
  F. DeluxeThingy has-a Component.


解答


Ans: A F

解說:

Meter繼承Component
DeluxeThingy 繼承Thingy ,Thingy 中有一個Meter物件(也是Component物件),滿足F
因多形原則,m物件屬於Meter,因此二個go方法皆為Meter的go,印出m