class Father{
constructor(name,age){
this.name=name;
this.age=age;}
hello(){
console.log("我的名字是"+this.name);
}
}
class Son extends Father{
constructor(name,age,hobby){ //hobby是子类独有而父类没有的,也要记得写上
super(name,age); //调用super方法让子类获取this对象
this.hobby=hobby; //子类独有
}
nianling(){ //下面两个为子类独有的方法
console.log("我的年龄是"+this.age);
}
aihao(){
console.log("我的爱好是"+this.hobby);
}
}
var myson = new Son('alexios','22','play');
myson.hello();
myson.nianling();
myson.aihao();
//子类既可以使用父类的方法,也可以使用自己的方法