//01-类的定义 class Person { //构造函数 只允许一个构造器 constructor(name,age) { this.name=name; this.age=age; } //类的方法 say(){ console.log(`this name is:${this.name} ,age: ${this.age} `); // 反引号的应用 tab+上面的那个键同时按 } sing(){ console.log("sing is runing.....") } } //end Person let person1 = new Person("张三丰",120); person1.say();//this name is:张三丰 ,age: 120 //02-类的继承 class Man extends Person{ //继承 //子类不写构造器经测试也是可以的,它自动继承父类的。 constructor(name,age) { super(name,age); //继承父类 } //子类方法,父类中不存在 play() { console.log(`${this.name} now start playing...... `); // 反引号的应用 tab+上面的那个键同时按 } //如果这里也定义了say()方法和主类相同的话,主类方法将被过载。 // say() { // console.log("我重载了父类的方法!") // }
Nodejs进阶系列-03- 类的定义
最新推荐文章于 2022-09-08 19:58:26 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)