赞
踩
ES6 Class(类)的继承通过extends关键字实现。在子类的方法中通过super关键字调用父类的方法。
- class Person {
- constructor(name) {
- this.name = name;
- }
- sayHi() {
- console.log(`This is ${this.name}`);
- }
- }
- class Student extends Person {
- constructor(name, number) {
- super(name); // 调用父类的constructor()
- this.number = number;
- }
- say() {
- super.sayHi(); // 调用父类的sayHi()
- console.log(`${this.name}'s number is ${this.number}`);
- }
- }
- const s = new Student('Lily', '1001');
- s.say();
-
- // This is Lily
- // Lily's number is 1001
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。