es6:定义People人类,包含姓名,性别,年龄,身高,家庭地址属性和跑步(run)方法; 定义Student学生类继承People类,并含有自身的学校,班级,学号属性和学习(study)方法; 定义Employee员工类继承People类,并含有自身的工作单位,部门,工号属性和工作(work)方法。 实例化学生类和员工类,并输出父类及子类定义的所有信息。
时间: 2023-07-10 15:03:46 浏览: 69
```javascript
class People {
constructor(name, gender, age, height, address) {
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
this.address = address;
}
run() {
console.log(`${this.name} is running.`);
}
}
class Student extends People {
constructor(name, gender, age, height, address, school, className, studentID) {
super(name, gender, age, height, address);
this.school = school;
this.className = className;
this.studentID = studentID;
}
study() {
console.log(`${this.name} is studying.`);
}
}
class Employee extends People {
constructor(name, gender, age, height, address, company, department, employeeID) {
super(name, gender, age, height, address);
this.company = company;
this.department = department;
this.employeeID = employeeID;
}
work() {
console.log(`${this.name} is working.`);
}
}
const student1 = new Student(
"Tom",
"male",
18,
170,
"123 Main St",
"ABC School",
"Class A",
"20210001"
);
const employee1 = new Employee(
"Alice",
"female",
30,
165,
"456 Park Ave",
"XYZ Company",
"Marketing",
"1001"
);
console.log(student1);
console.log(employee1);
```
输出结果:
```javascript
Student {
name: 'Tom',
gender: 'male',
age: 18,
height: 170,
address: '123 Main St',
school: 'ABC School',
className: 'Class A',
studentID: '20210001'
}
Employee {
name: 'Alice',
gender: 'female',
age: 30,
height: 165,
address: '456 Park Ave',
company: 'XYZ Company',
department: 'Marketing',
employeeID: '1001'
}
```