进阶1先用js来写个类并使用
class Student{
constructor(name,score){
this.name=name;
this.score=score;
}
introduce(){
console.log(`我是${this.name},考了${this.score}分`);
}
}
const student=new Student('张三',99)
console.log('student',student);
student.introduce();
输出
进阶2类继承
class Person{
constructor(name){
this.name=name;
}
drink(){
console.log('喝水');
}//sudent和教师也能继承到方法
}
class Student extends Person{
constructor(name,score){
super(name)
this.score=score;
}
introduce(){
console.log(`我是${this.name},考了${this.score}分`);
}
}
const student=new Student('进阶2',100)
student.drink()
class Teacher extends Person{
constructor(name,subject){
super(name)
this.subject=subject;
}
introduce(){
console.log(`我是${this.name},教${this.score}`);
}
}
输出