Given that:
Gadget has-a Sprocket and
Gadget has-a Spring and
Gadget is-a Widget and
Widget has-a Sprocket
Which two code fragments represent these relationships? (Choose two.)
A. class Widget{Sprocket s;}
class Gadget extends Widget{Spring s;}
B. class Widget{}
class Gadget extends Widget{Spring s1; Sprocket s2;}
C. class Widget{Sprocket s1; Spring s2;}
class Gadget extends Widget{}
D. class Gadget{Spring s;}
class Widget extends Gadget{Sprocket s;}
E. class Gadget{}
class Widget extends Gadget{Sprocket s1; Spring s2;}
F. class Gadget{Spring s1; Sprocket s2;}
class Widget extends Gadget{}
答案:AC
參考:6-5 繼承
-------------------------------------------------
A: class Widget{Sprocket s;} //Widget has-a Sprocket
class Gadget extends Widget{Spring s;} //Gadget has-a Spring, also extend Sprocket from Widget, so Gadget is-a Widget and Gadget has-a Sprocket.
C. class Widget{Sprocket s1; Spring s2;} //Widget has-a Sprocket
class Gadget extends Widget{} //Gadget extend both Sprocket and Spring from Widget,
so Gadget has-a Sprocket and Gadget has-a Spring and Gadget is-a Widget.
沒有留言:
張貼留言