如何更改子类内部的superClass属性?

时间:2017-11-09 19:55:18

标签: python python-3.x class

当我定义__init__的{​​{1}}时,我还需要设置ProductionWorker的属性。我输入EmployeeClass"Bob"作为测试,它可以正常工作,但我需要能够在用户的输入中更改主要内容。

"001121"

我的主要人物:

class ProductionWorker(EmployeeClass):
    SHIFT = {1: "day shift", 2: "night shift"}

    def __init__(self, shift=None, hourly_pay=None):
        EmployeeClass.__init__(self,  "Bob", "001121")
        self.__shift = shift
        self.set_shift = shift
        self.__hourly_pay = hourly_pay
        self.set_hourly_pay = hourly_pay
    # setters

    def set_shift(self, shift):
        if shift in ProductionWorker.SHIFT:
            self.__shift = shift
        else:
            self.__shift = None

    def set_hourly_pay(self, hourly_pay):
        self.__hourly_pay = hourly_pay
    # getters

    def get_shift(self):
        return self.__shift

    def get_hourly_pay(self):
        return self.__hourly_pay

    def __str__(self):
        summary = EmployeeClass.__str__(self)
        return summary + "They work on the " + ProductionWorker.SHIFT[self.__shift] + " and make " + "$" \
               + str(format(self.__hourly_pay, "0.2f")) + " an hour."

这是我得到的结果:

from Employee import EmployeeClass
from Employee import ProductionWorker

e_name = input("Enter the name of the employee: ")
e_number = input("Enter the ID number of the employee: ")
e_shift = int(input("Enter 1 if they work day shift or 2 if they work night shift: "))
e_hourly_pay = float(input("Enter how much they make hourly (numerical): "))

x = EmployeeClass(e_name, e_number)
z = ProductionWorker(e_shift, e_hourly_pay)
print(z)

2 个答案:

答案 0 :(得分:1)

您必须将参数与任何其他参数一起使用:

class ProductionWorker(EmployeeClass):
    SHIFT = {1: "day shift", 2: "night shift"}

    def __init__(self, name, number, shift=None, hourly_pay=None):
        EmployeeClass.__init__(self,  name, number)
        self._shift = shift
        self.hourly_pay = hourly_pay

    @property
    def shift(self):
        return self._shift

    @shift.setter
    def shift(self, shift):
        if shift in ProductionWorker.SHIFT:
            self._shift = shift
        else:
            self._shift = None

    def __str__(self):
        summary = EmployeeClass.__str__(self)
        return summary + "They work on the {} and make ${:.2f} an hour.".format(
            ProductionWorker.SHIFT[self.shift], self.hourly_pay)

name = input("Enter the name of the employee: ")
number = input("Enter the ID number of the employee: ")
shift = int(input("Enter 1 if they work day shift or 2 if they work night shift: "))
hourly_pay = float(input("Enter how much they make hourly (numerical): "))

z = ProductionWorker(name, number, shift, hourly_pay)
print(z)

答案 1 :(得分:1)

我会在ProductionWorker的init方法参数中包含EmployeeClass的参数,以传递给超类。

对于python 3,你可以做super().__ init ___()而不是EmployeeClass .__ init __()。

此外,您应该考虑使用descriptors而不是实现getter和setter,因为这是pythonic方法。

class ProductionWorker(EmployeeClass):
    def __init__(self, name, number, shift=None, hourly_pay=None):
        super().__init__(name, number)
        self.__shift = shift
        self.__hourly_pay = hourly_pay