python方法在课外获取信息

问题描述 投票:0回答:3

--EDIT--添加了我的Employee类

方法只处理类中的信息(属性)吗?可以使用方法处理来自外部的信息吗?我怎样才能“宣传”莎拉?所以我必须创建sarah员工实例,但我如何让经理来推广她呢?

class Employee:
    """Base infromation about any emploee"""

    def __init__(self, name, last_name, birthdate, email, phone, 
                    social_sec_no, credit_card, level):
        self.name = name
        self.last_name = last_name
        self.birthdate = birthdate
        self.email = email
        self.phone = phone
        self.social_sec_no = social_sec_no
        self.credit_card = credit_card
        self.level = level #level 1-3 for laborer, 4- administration, 5 - plant manager

class PlantManager(Employee):
    """Plant manager:
        - Approves budget
        - Promotes
        - Gives order to hire - fire person
    """

    def __init__(self, name, last_name, birthdate, email, phone,
                    social_sec_no, credit_card, level):
        super().__init__(name, last_name, birthdate, email, phone,
                    social_sec_no, credit_card, level)


    def promotion(self, employee_lvl):
        if employee_lvl == 3: 
            return
        else:
            employee_lvl = employee_lvl + 1

manager = PlantManager('John', 'Stockton', '1989-05-15', '[email protected]', '+17068645474',
                '5847-487-0', '222 484 999', 5)

sarah = 1

print(manager.promotion(sarah)) #returns None
python python-3.x
3个回答
1
投票

如果要提升员工,则需要通过提供员工来指定哪一个员工。

class PlantManager(Employee):

    ...

    def promotion(self, employee):
        if employee.level >= 3: 
            raise ValueError('not allowed to promote') # better to raise than silently fail
        else:
            employee.level += 1

然后你可以推广它们。

sarah = Employee('Sarah', 'Connor', '1965-11-13', '[email protected]', '123', '456', '789', 1)

manager.promote(sarah) # after all she went through

sarah.level # 2

0
投票

您的函数提升不会返回else部分中的值


0
投票

你必须制作一个名为Sarah的对象,例如:

Sarah = Employee('sarah', 'name', '1989-05-15', '[email protected]', '+17068645474',
            '5847-487-0', '222 484 999', 5)

然后你可以调用促销方法来提高等级:

Sarah.level = Manager.promote(Sarah.level)

还应编辑PlantManager的提升方法,以便返回最终值。

编辑了新信息。

© www.soinside.com 2019 - 2024. All rights reserved.