How do I use composition with inheritance?

前端 未结 10 2522
星月不相逢
星月不相逢 2021-02-09 04:41

I\'m going to try to ask my question in the context of a simple example...

Let\'s say I have an abstract base class Car. Car has-a basic Engine object. I have a method

10条回答
  •  情话喂你
    2021-02-09 05:17

    There are lots of ways it could be done.

    I would favour having a setEngine() method on Car, then having the Ferrari constructor call setEngine() and pass in an instance of a TurboEngine.

提交回复
热议问题