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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

子類重寫了父類的方法為什么還是會(huì)報(bào)錯(cuò)

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

Beverage.prototype.brew?=?function(){
	//?console.log("沖泡飲料");
	throw?new?Error("子類必須重寫該方法");
};

Beverage.prototype.putInCup?=?function(){
	//?console.log("倒進(jìn)杯中");
	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("把咖啡倒進(jìn)杯子里");
}
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("把茶倒進(jìn)杯子里");
}
Tea.prototype.addCondiments=?function?()?{
??console.log("加檸檬");
}
Tea.prototype.customIsWant?=?function(){
	return?window.confirm("請(qǐng)問需要加檸檬嗎?");
}

//?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("把咖啡倒進(jìn)杯子");

????????};

????????Tea.prototype.addCondiments?=?function()?{

????????????console.log("加檸檬");

????????};

????????Tea.prototype.customerWantsCondiments?=?function()?{

????????????return?window.confirm("請(qǐng)問是否需要添加調(diào)料");

????????};

????????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("把咖啡倒進(jìn)杯子");

????????};

????????Coffee.prototype.addCondiments?=?function()?{

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

????????};





????????var?tea?=?new?Tea();

????????tea.init();

????????var?coffee?=?new?Coffee();

????????coffee.init();


0 回復(fù) 有任何疑惑可以回復(fù)我~

繼承也可以寫成 Coffee.prototype = Object.create(Beverage.prototype); ?

Object.create()創(chuàng)建一個(gè)空對(duì)象,這個(gè)空對(duì)象的原型指向Beverage.prototype,將Coffee的prototype=這個(gè)空對(duì)象的原型,實(shí)現(xiàn)一個(gè)繼承關(guān)系,


0 回復(fù) 有任何疑惑可以回復(fù)我~

代碼順序問題,應(yīng)該先實(shí)現(xiàn)繼承,然后再擴(kuò)寫子類的方法。你的代碼中先擴(kuò)寫了原型對(duì)象的方法,然后再寫繼承Coffee.prototype?=?new?Beverage(); ?這樣的話之前寫的方法就被覆蓋了,所以重寫并沒有實(shí)現(xiàn)


4 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

子類重寫了父類的方法為什么還是會(huì)報(bào)錯(cuò)

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

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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