```
//定义一个People类
class People {
constructor(name.age);
this.name = name; //属性
this.age = age; //属性
eat() { //方法
alert(`${this.name} eat something`)
}
speak() { //方法
alert(`My name is ${this.name}, age ${this.age}`)
}
}
//new一个People,那么new的类也有People的方法和属性
let zhang = new People('zhang', 20);
zhang.age;
zhang.eat();
zhang.speak();
let wang = new People('wang', 21)
wang.eat()
wang.speak()
```
```
class People {
constructor(name, age) {
this.name = name
this.age = age
}
eat() {
alert(`${this.name} eat something`)
}
speak() {
alert(`My name is ${this.name}, age ${this.age}`)
}
}
class Student extends People {
constructor(name, age, number) {
super(name, age);//继承自父类
this.number = number //自有的属性
}
study() {
alert(`${this.name} study`)
}
}
let xiaoming = new Student('xiaoming', 10, 'A1')
xiaoming.study()
console.log(xiaoming.number)
let xiaohong = new Student('xiaohong', 11, 'A2')
xiaohong.study()
```
* 多态
保持子类的开放性和灵活性
面向接口编程