经典的饮料与咖啡
先定义父类,子类继承通用的方法,特殊的方法在子类重写。
//父类咖啡
class Beverage {
constructor(name) {
//单独调用会报错,所以写constructor里面绑定this,this指向父类
this.init = () => {
this.boilWater();
this.brew();
this.pourInCup();
if (this.customerWantsCondiments()) { // 如果挂钩返回 true,则需要调料
this.addCondiments();
}
};
console.log('构造函数的'+name)
};
//共用boilWater,子类不会修改它
boilWater() {
console.log('把水煮沸');
};
brew() {
throw new Error('子类必须重写 brew 方法');
};
pourInCup() {
throw<