python 实现 设计一个部门类Department,属性为部门名称(name)

设计一个部门类Department,属性为部门名称(name)

需求: 1.直接通过Department调用get_all方法,返回公司员工总数 2.通过部门实例,调用方法get_count方法,返回部门员工数 3.通过部门实例,调用方法get_employees(),显示部门所有员工信息 格式为: 工号-xxx ,姓名-xxx ,手机号-xxx,工资-xxx 提示:有人入职,员工数就要增加,部门存入员工对象 设计一个员工类Employee,员工属性有姓名(name)、手机号(phone)、工资(salary)、所属部门。 需求: 1.新员工入职时,分配一个不重复的工号。从1开始排列,已离职员工工号作废。 2.员工本人可以调用get_salary方法查询自己的工资 3.员工本人可以调用get_info方法查询自己的全部信息--字典形式



#!/usr/bin/python

class Department(object):
	count = 0
	
	def __init__(self,name):
		self.name = name
		self.departmentcount=0
		self.employees = []
		
	@classmethod
	def get_all(cls):
		return Department.count
	
	def get_count(self):
		return self.departmentcount
		
	def get_employees(self):
		info =""
		for i in range(len(self.employees)):
			info+=self.employees[i].get_chinaInfo()+"\n"
		return info
	
	def get_name(self):
		return self.name

class Employee(object):
	
	def __init__(self,name,phone,salary,department):
		self.name = name
		self.phone = phone
		self.salary = salary
		self.department = department
		department.employees.append(self)
		Department.count = Department.count+1
		department.departmentcount += 1
		self.id = Department.count
		
		
		
		
	def get_salary(self):
		return self.salary
	
	def get_info(self):
		return f'"id":{self.id},"name":{self.name},"phone":{self.phone},"salary":{self.salary},"department":{self.department.get_name()}'
	
	def get_chinaInfo(self):
		return f'工号-{self.id},姓名-{self.name},手机号-{self.phone},工资-{self.salary}'
	
HR = Department("HR")
TECH = Department("TECH")
HR1 = Employee("小兰",18513622992,1000,HR)
HR2 = Employee("小红",18513622991,2000,HR)
TECH1 = Employee("小花",18513622993,3000,TECH)
TECH2 = Employee("小海",18513622994,4000,TECH)
print(HR1.get_info())
print(HR2.get_info())
print(TECH1.get_info())
print(TECH2.get_info())
print(Department.get_all())
print(HR.get_count())
print(HR.get_employees())