Appearance
类
脚本内容。
// 父类
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduce() {
return `大家好,我是${this.name},今年 ${this.age} 岁。`;
}
}
// 子类
class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}
introduce() {
const baseInfo = super.introduce();
return `${baseInfo}我是一名${this.grade}年级的学生。`;
}
study() {
return `${this.name}正在学习...`;
}
}
// 实例
const p1 = new Person('张三', 30);
console.log(p1.introduce());
const s1 = new Student('李强', 15, '高一');
console.log(s1.introduce());
console.log(s1.study());大家好,我是张三,今年 30 岁。
大家好,我是李强,今年 15 岁。我是一名高一年级的学生。
李强正在学习...