ECMA6class语法
回顾:JS继承和多态
//ECMA6class语法
class Person{
//class属性添加
constructor(name,sex,age){
this.name = name;
this.sex = sex;
this.age = age;
}
ShowSelf(){
alert(`我是一个叫${this.name},今年${this.age}岁的${this.sex}孩`)
}
}
var p1 = new Person("blue","男",18);
p1.ShowSelf(); // 我是一个叫blue,今年18岁的男孩
//extends 继承
class Worker extends Person{
constructor(name,sex,age,job){
//1.继承到父一级的属性
super(name,sex,age);
this.job = job;
}
showJob(){
alert("我的工作是" + this.job);
}
}
var w1 = new Worker("小米","男",20,"程序员");
w1.ShowSelf(); // 我是一个叫小米,今年20岁的男孩
w1.showJob(); // 我的工作是程序员