我有一个Employee类和一个Manager类,创建的每个雇员都将获得Employee类甚至Manager的属性。在
我的问题是,我想创建一个输入选项,让经理输入他想添加到其监督中的员工(将其添加到员工列表中),并且还将添加employees属性 (我知道最后3条线路有问题,我就是想不通)。在
class Employee:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
self.email = first+'.'+last+'@company.com'
def fullname(self):
return '{} {}'.format(self.first,self.last)
class Manager(Employee):
def __init__(self,first,last,pay,employees=None):
super().__init__(first,last,pay)
if employees is None:
self.employees = []
else:
self.employees = employees
def add_emps(self,emp):
if emp not in self.employees:
self.employees.append(emp)
else:
print('the employee is already in your supervise')
def print_emps(self):
for em in self.employees:
print('-->',em.fullname())
emp_1 = Employee('Mohamad','Ibrahim',90000)
emp_2 = Employee('Bilal','Tanbouzeh',110000)
emp_3 = Employee('Ghalia','Awick',190000)
emp_4 = Employee('Khaled','Sayadi',80000)
mngr_1 = Manager('Ibrahim','othman',200000,[emp_1,emp_2])
mngr_2 = Manager('Rayan','Mina',200000,[emp_3,emp_4])
add_them = input('enter the employee you would like to add')
mngr_1.add_emps(add_them)
mngr_1.print_emps()
如果您不熟悉词典,我将简要介绍一下,但是您应该仔细阅读PyDocs,以及{a2}上的一般维基百科条目。在
现在来回答你的实际问题。我构建了一个员工字典,并从字典中添加了员工的密钥给经理。我还展示了两种构造字典的方法:一种是在创建
^{pr2}$dict
时添加值,另一种是稍后添加值。在相关问题 更多 >
编程相关推荐