OO class 层次结构

OO class hierarchy

最近收到了这个面试题。 "Please give the OO hierarchy of following classes. Bicycle, Car, Truck, Engine, and Wheel"。由于没有反馈,我不知道正确答案应该是什么。你们有什么感想?为什么?非常感谢。

I don't know what should be the right answer.

有多种方法可以对此进行建模。各种 "right" 答案。例如:

class VehiclePart

class Wheel extends VehiclePart

class Engine extends VehiclePart

class Vehicle {
  Wheel[] wheel
}

class PoweredVehicle extends Vehicle {
   Engine: engine
}

class Bicyle extends Vehicle 

class Car extends PoweredVehicle

class Truck extends PoweredVehicle

我正在使用我刚刚为这个答案弥补的文本符号。如果手边有一块白板,我会把它画成 UML class 图……或类似的图。

And why

因为它充分代表了 classes 和关系。其他模型也会或可能...取决于模型的用途等等。

面试问题的重点不是看你能不能生产"the right answer",而是看你是否对 OO 建模有基本的掌握。