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>
运行结果:
------------恢复内容结束------------