为了账号安全,请及时绑定邮箱和手机立即绑定

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

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 回答

老师的代码有问题

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();


0 回复 有任何疑惑可以回复我~

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

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


0 回复 有任何疑惑可以回复我~

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


4 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号