一、繼承: 能夠將公用的方法抽離出來放在一塊兒,進行屢次服用,節省代碼量!javascript
1.一、建立類模式java
// 類,就是模板學習
class Person{ constructor(name,age,food){ this.name = name, this.age = age, this.food = food } eat(){ console.log(`${this.name} love eat ${this.food}`) } speak(){ console.log(`${this.name} say: my name is ${this.name},age is ${this.age}`) } } class Student extends Person{ //extends是繼承的關鍵字,Student繼承了Person的屬性和方法 constructor(name, age, food,grade){ super(name, age, food), //和 Person.call(this,name, age, food) 原理同樣 this.grade = grade } study(){ console.log(`${this.name}正在學習,別打擾!,他要參加${this.grade}考試!`) } } let zhang = new Person('zhang',21,'桃子'); zhang.eat(); zhang.speak(); let wang = new Person('wang',25,'葡萄'); wang.eat(); wang.speak(); let li = new Student('li', 23, '橘子','3年級') li.eat(); li.speak(); li.study(); let honghong = new Student('honghong', 23, '蘋果', '2年級') honghong.eat(); honghong.speak(); honghong.study();