在ES6中,继承的方法就更加方便了,废话不多说,直接上代码
// 父类
class Parent {
// 构造父类的属性
constructor(name, age){
this.name = name;
this.age = age;
}
// 创建父类的方法
say() {
alert('我叫' + this.name + ',我今年' + this.age);
}
}
// 子类
class Son extends Parent {
// 继承父类的属性和方法
constructor(name, age, sex){
// 调用super方法访问父对象上的构造函数
super(name, age);
// 新增属性
this.sex = sex;
}
// 新增方法
say2(){
alert('我是中国人!');
}
}
// 实例化一个son1对象
let son1 = new Son('张三', 18, '男')
son1.say()
son1.say2()