子类重写了父类的方法为什么还是会报错

来源:4-3 模板方法模式的基本知识

冖儿

2017-07-11 10:48

var Beverage = function(){}
Beverage.prototype.boilWater = function(){
	console.log("煮沸水");
};

Beverage.prototype.brew = function(){
	// console.log("冲泡饮料");
	throw new Error("子类必须重写该方法");
};

Beverage.prototype.putInCup = function(){
	// console.log("倒进杯中");
	throw new Error("子类必须重写该方法");
};

Beverage.prototype.addCondiments = function(){
	// console.log("添加辅料");
	throw new Error("子类必须重写该方法");
};
Beverage.prototype.customIsWant = function(){
	// 演示钩子方法
	return true;
}
Beverage.prototype.init = function(){
	this.boilWater();
	this.brew();
	this.putInCup();
	if(this.customIsWant()){
		this.addCondiments();
	}
}

var Coffee = function(){}

Coffee.prototype.brew = function () {
  console.log("用开水冲泡咖啡");
}
Coffee.prototype.putInCup= function () {
  console.log("把咖啡倒进杯子里");
}
Coffee.prototype.addCondiments= function () {
  console.log("加糖和牛奶");
}
Coffee.prototype.customIsWant = function(){
	return false;
}

var Tea = function(){}

Tea.prototype.brew = function () {
  console.log("用开水冲泡茶");
}
Tea.prototype.putInCup= function () {
  console.log("把茶倒进杯子里");
}
Tea.prototype.addCondiments= function () {
  console.log("加柠檬");
}
Tea.prototype.customIsWant = function(){
	return window.confirm("请问需要加柠檬吗?");
}

// js的继承
Coffee.prototype = new Beverage(); 

Tea.prototype = new Beverage();

var coffee = new Coffee();
console.log(coffee.brew());
coffee.init();

var tea = new Tea();
tea.init();


写回答 关注

3回答

  • 好孩子100分
    2019-09-29 15:29:08

    老师的代码有问题

    var Beverage = function() {};

            Beverage.prototype.boilWater = function() {

                console.log("把水煮沸");

            };

            Beverage.prototype.brew = function() {

                throw new Error('子类必须重写该方法');

            };

            Beverage.prototype.pourInCup = function() {

                throw new Error('子类必须重写该方法');

            };

            Beverage.prototype.addCondiments = function() {

                throw new Error('子类必须重写该方法');

            };

            Beverage.prototype.customerWantsCondiments = function() {

                return true;

            };

            Beverage.prototype.init = function() {

                this.boilWater();

                this.brew();

                this.pourInCup();

                if (this.customerWantsCondiments()) {

                    this.addCondiments();

                }


            };

            var Tea = function() {};

            Tea.prototype = new Beverage();

            Tea.prototype.boilWater = function() {

                console.log("把水煮沸");

            };

            Tea.prototype.brew = function() {

                console.log("用沸水冲泡茶叶");

            };

            Tea.prototype.pourInCup = function() {

                console.log("把咖啡倒进杯子");

            };

            Tea.prototype.addCondiments = function() {

                console.log("加柠檬");

            };

            Tea.prototype.customerWantsCondiments = function() {

                return window.confirm("请问是否需要添加调料");

            };

            var Coffee = function() {};

            Coffee.prototype = new Beverage();

            Coffee.prototype.boilWater = function() {

                console.log("把水煮沸");

            };

            Coffee.prototype.brew = function() {

                console.log("用沸水冲泡咖啡");

            };

            Coffee.prototype.pourInCup = function() {

                console.log("把咖啡倒进杯子");

            };

            Coffee.prototype.addCondiments = function() {

                console.log("加糖和牛奶");

            };





            var tea = new Tea();

            tea.init();

            var coffee = new Coffee();

            coffee.init();


  • 一飞同学
    2017-09-19 14:35:27

    继承也可以写成 Coffee.prototype = Object.create(Beverage.prototype);  

    Object.create()创建一个空对象,这个空对象的原型指向Beverage.prototype,将Coffee的prototype=这个空对象的原型,实现一个继承关系,


  • 慕粉3777950
    2017-07-14 18:24:01

    代码顺序问题,应该先实现继承,然后再扩写子类的方法。你的代码中先扩写了原型对象的方法,然后再写继承Coffee.prototype = new Beverage();  这样的话之前写的方法就被覆盖了,所以重写并没有实现


星级评分原理和实现(上)

本课程主要讲解如何使用不同的方式来实现星级评分的效果.

25805 学习 · 119 问题

查看课程

相似问题