[英]javascript: class inheritance method call
我是 JS 新手,我對以下代碼特別困惑:
class One{
constructor(num){
this.num = num;
}
add(){
this.num += 1;
}
showInfo(){
this.add();
console.log(`The result is ${this.num}`);
}
}
class Ten extends One{
constructor(num){
super(num);
}
add(){
this.num += 10;
}
}
let t = new Ten(1);
t.showInfo();
// The result is 11
我認為結果應該是 2,因為showInfo
在One
執行,所以add
應該在One
執行,而且showInfo
只在One
定義,但為什么One.showInfo
調用Ten.add
而不是One.add
?
如果有人提供一些解釋,我會很高興。
showInfo()由Ten類繼承,在Ten類上調用Ten類的add()執行; 產量 11;
返回 2 add() 在十類中的實現應該調用 super.add() 代替
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.