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 () { super.drive() 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() { super.drive() console.log('Speeding through the water with ease!') } }
Car 重写了继承自父类的 drive() 方法,但是之后 Car 调用了 super.drive() 方法,这表明 Car 可以引用继承来的原始 drive() 方法。快艇的 pilot() 方法同样引用了原始 drive() 方法。