如何将员工加入经理的主管?(Python类)

2024-09-24 22:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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()

Tags: inselfaddinitdefemployeemanagerpay
1条回答
网友
1楼 · 发布于 2024-09-24 22:25:31

如果您不熟悉词典,我将简要介绍一下,但是您应该仔细阅读PyDocs,以及{a2}上的一般维基百科条目。在

a = {} # create an empty dictionary
a['some_key'] = "Some value" # Equivalent of creating it as a = {'some_key': "Some value"}
# Dictionaries are stored in "key, value pairs" that means one key has one value.
# To access the value for a key, we just have to call it
print(a['some_key'])
# What if we want to print all values and keys?
for key in a.keys():
    print("Key: " + key + ", Value: " + str(a[key]))

现在来回答你的实际问题。我构建了一个员工字典,并从字典中添加了员工的密钥给经理。我还展示了两种构造字典的方法:一种是在创建dict时添加值,另一种是稍后添加值。在

^{pr2}$

相关问题 更多 >