es6编程题: 定义一个科室,要求存储科室名称、人数、类别,科员信息(包括姓名、年龄、性别、等级),并提供增加科员,科员显示,获得科室人数的方法
时间: 2023-12-22 11:00:38 浏览: 52
todomvc-es6-jspm:一个用 jspm 用 ES6 编写的 todomvc
class Department {
constructor(name, numberOfPeople, category) {
this.name = name;
this.numberOfPeople = numberOfPeople;
this.category = category;
this.staff = [];
}
addStaff(name, age, gender, level) {
this.staff.push({
name: name,
age: age,
gender: gender,
level: level
});
this.numberOfPeople++;
}
showStaff() {
console.log("Staff in " + this.name + ":");
this.staff.forEach((staff) => {
console.log(staff.name + " - " + staff.age + " - " + staff.gender + " - " + staff.level);
});
}
getNumberOfPeople() {
return this.numberOfPeople;
}
}
// Example usage:
const department = new Department("Cardiology", 10, "Medical");
department.addStaff("John Doe", 30, "Male", "Senior");
department.addStaff("Jane Smith", 25, "Female", "Junior");
department.showStaff();
console.log(department.getNumberOfPeople()); // Output: 12 (2 staff members were added)
阅读全文