人员类,员工类,员工表类的定义和实现,并设计主程序进行测试
时间: 2024-05-13 07:17:11 浏览: 100
以下是人员类、员工类和员工表类的定义和实现,以及主程序的设计和测试。
1. 人员类的定义和实现
```python
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def __str__(self):
return f"{self.name} ({self.gender}, {self.age} years old)"
```
2. 员工类的定义和实现
```python
class Employee(Person):
def __init__(self, name, age, gender, id, department, salary):
super().__init__(name, age, gender)
self.id = id
self.department = department
self.salary = salary
def __str__(self):
return super().__str__() + f", {self.department} department, ID:{self.id}, Salary:{self.salary}"
def increase_salary(self, amount):
self.salary += amount
```
3. 员工表类的定义和实现
```python
class EmployeeTable:
def __init__(self):
self.employees = []
def add_employee(self, employee):
self.employees.append(employee)
def remove_employee(self, id):
for employee in self.employees:
if employee.id == id:
self.employees.remove(employee)
return True
return False
def find_employee(self, id):
for employee in self.employees:
if employee.id == id:
return employee
return None
def get_all_employees(self):
return self.employees
```
4. 主程序设计和测试
```python
# 创建员工表
employees = EmployeeTable()
# 添加员工
employees.add_employee(Employee("Tom", 25, "male", "001", "sales", 5000))
employees.add_employee(Employee("Lucy", 30, "female", "002", "marketing", 6000))
employees.add_employee(Employee("John", 35, "male", "003", "finance", 7000))
# 输出所有员工信息
print("All employees:")
for employee in employees.get_all_employees():
print(employee)
# 增加员工薪水
employee = employees.find_employee("002")
if employee:
employee.increase_salary(1000)
print(f"Employee {employee.id}'s salary increased to {employee.salary}")
# 删除员工
if employees.remove_employee("001"):
print("Employee 001 removed")
else:
print("Employee 001 not found")
# 重新输出所有员工信息
print("All employees:")
for employee in employees.get_all_employees():
print(employee)
```
输出结果:
```
All employees:
Tom (male, 25 years old), sales department, ID:001, Salary:5000
Lucy (female, 30 years old), marketing department, ID:002, Salary:6000
John (male, 35 years old), finance department, ID:003, Salary:7000
Employee 002's salary increased to 7000
Employee 001 removed
All employees:
Lucy (female, 30 years old), marketing department, ID:002, Salary:7000
John (male, 35 years old), finance department, ID:003, Salary:7000
```
阅读全文