es6编程题: 定义一个科室,要求存储科室名称、人数、类别,科员信息(包括姓名、年龄、性别、等级),并提供增加科员,科员显示,获得科室人数的方法
时间: 2023-05-27 22:04:12 浏览: 74
todomvc-es6-jspm:一个用 jspm 用 ES6 编写的 todomvc
class Department {
constructor(name, number, category) {
this.name = name;
this.number = number;
this.category = category;
this.staffs = [];
}
addStaff(staff) {
this.staffs.push(staff);
}
showStaffs() {
this.staffs.forEach(staff => {
console.log(`姓名: ${staff.name} 年龄: ${staff.age} 性别: ${staff.gender} 等级: ${staff.level}`);
});
}
getNumber() {
return this.number;
}
}
// 定义员工类
class Staff {
constructor(name, age, gender, level) {
this.name = name;
this.age = age;
this.gender = gender;
this.level = level;
}
}
// 创建一个科室实例
const department = new Department('内科', 20, '医疗');
// 添加员工
department.addStaff(new Staff('张三', 30, '男', '高级'));
department.addStaff(new Staff('李四', 25, '女', '初级'));
// 显示员工
department.showStaffs();
// 获取人数
console.log(department.getNumber()); // 20
阅读全文