class Vehicle { constructor() { this.engines = 1 } ignition () { console.log(‘Turning on my engine‘); } drive () { this.ignition(); console.log(‘Steering and moving forward!‘) } } class Car extends Vehicle { constructor() { super() this.wheels = 4 } drive () { this.ignition() console.log(‘Rolling on all ‘, this.wheels, ‘ wheels‘) } } class SpeedBoat extends Vehicle { constructor() { super(); this.engines = 2 } ignition () { console.log(‘Turning on my ‘, this.engines, ‘ engines.‘); } pilot() { this.ignition(); console.log(‘Speeding through the water with ease!‘) } }
我们通过定义 Vehicle 类来假设一种发动机,一种点火方式,一种驾驶方式。这个类是一个抽象的概念。接下来定义了两类具体的交通工具:Car 和 SpeedBoat。它们都从 Vehicle 继承了通用的特性并根据自身类别修改了某种特性。汽车需要 4 个轮子,快艇需要 2 个发动机,因此它必须启动 2 个发动机的点火装置
原文:https://www.cnblogs.com/wzndkj/p/12632945.html