//定义一个Person类
class Person {
//构造方法
constructor(name,age) {
//构造器中的this是谁? -------当前类的实例对象
this.name = name;
this.age = age;
}
//一般方法
/*
1.say放在那里? ----Person的原型对象上,供实例使用
2.say中的this是谁? ----如果say是通过Person实例调用的,那么this就是Person的实例对象
*/
say() {
console.log(我叫${this.name},今年${this.age}
)
}
}
//定义一个Person的实例对象
var p =new Person(‘guo’,18);
console.log§
console.log(p.name);
p.say();
//定义一个Student类继承于Person
class Student extends Person{
constructor(name,age,greate) {
super(name,age)
this.greate = greate
}
//重写从父类继承过来的方法
say() {
console.log(我叫${this.name},今年${this.age},读${this.greate}
)
}
/*
1.study放在那里? ----Student的原型对象上,供实例使用
2.study中的this是谁? ----如果study是通过Student实例调用的,那么this就是Student的实例对象
*/
study() {
console.log(“我在努力学习”,this)
}
}
let s = new Student(‘hang’,19,‘二年级’);
console.log(s)
s.say()
s.study()