es6 子類繼承父類的方法同時擴展自己的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>子類繼承父類的方法同時擴展自己的方法</title> </head> <body> <script> class Father { constructor(x,y){ this.x =x; this.y =y; } sum(){ console.log(this.x+this.y) } } // 子類繼承父類的加法方法 同時擴展自己的減法方法 class Son extends Father { constructor(x,y){ //調用父類的構造方法 //super必須在子類this之前調用 super(x,y) this.x = x; this.y = y; } subtract() { console.log(this.x - this.y) } } var son = new Son(9,2) son.sum() son.subtract() </script> </body> </html>
運行結果:
------------恢復內容結束------------