car-analogy

Whether to model a car object (and its parts such as engine) with has-a (composition) or is-a (inheritance)?

北战南征 提交于 2019-12-07 08:28:42
问题 I am developing a class library which will include the object Car. The dilemma is, Car itself will be a class with fields such as Registration Number, and other general information on the car. But a car has an engine, chassis, etc. These objects need to be modelled too. Should they be classes embedded within Car? If not, what is the usage scenario of an embedded class? I've learnt that composition is "part of", so you can model seperate classes and use the engine type, for example, at the

How do I use composition with inheritance?

元气小坏坏 提交于 2019-12-03 08:16:36
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 StartEngine() in the abstract Car class that delegates the starting of the engine to the Engine object. How do I allow subclasses of Car (like Ferrari) to declare the Engine object as a specific type of engine (e.g., TurboEngine)? Do I need another Car class (TurboCar)? I'm inheriting a plain old Engine object and I cannot re-declare (or override) it as a TurboEngine in my Car subclasses. EDIT: I understand that I can plug any

What is a practical, real world example of the Linked List?

只愿长相守 提交于 2019-11-30 10:20:20
问题 I understand the definition of a Linked List, but how can it be represented and related to a common concept or item? For example, composition (EDIT: originally said 'inheritance') in OOP can be related to automobiles. All (most) automobiles in real life are the essentially same thing; an automobile has an Engine, you can start() it, you can make the car go(), stop() and so on. An automobile would typically have a maximum passenger capacity but it would differ between a Bus and a SportsCar,

What is a practical, real world example of the Linked List?

心不动则不痛 提交于 2019-11-29 19:57:54
I understand the definition of a Linked List, but how can it be represented and related to a common concept or item? For example, composition (EDIT: originally said 'inheritance') in OOP can be related to automobiles. All (most) automobiles in real life are the essentially same thing; an automobile has an Engine, you can start() it, you can make the car go(), stop() and so on. An automobile would typically have a maximum passenger capacity but it would differ between a Bus and a SportsCar, which are both automobiles. Is there some real life, intuitive example of the plain ole' singly Linked