第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Coffee.prototype = new Beverage(); 子類繼承了父類,還是報錯,怎么辦?

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


正在回答

1 回答

這里就舉這個coffe的例子了,應該是要先繼承父類,再重寫父類的方法。

??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?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?coffee?=?new?Coffee();
??coffee.init();


2 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

Coffee.prototype = new Beverage(); 子類繼承了父類,還是報錯,怎么辦?

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號