如何更改子类内部的superClass属性? [英] How do I change a superClass attribute that's inside of a subClass?

查看:160
本文介绍了如何更改子类内部的superClass属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我定义 ProductionWorker __ init __ 时,我还需要设置 EmployeeClass 。我输入了鲍勃 001121 作为测试,它可以工作,但我需要能够对其进行更改

When I define the __init__ of ProductionWorker, I also need to set the attributes of EmployeeClass. I entered "Bob" and "001121" as a test and it works but I need to be able to change it in my main from the input of the user.

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)

这是我得到的结果:

Enter the name of the employee: Joe
Enter the ID number of the employee: 5555
Enter 1 if they work day shift or 2 if they work night shift: 2
Enter how much they make hourly (numerical): 30

The employee's name is Bob. Bob's ID number is: 001121. They work on the night shift and make $30.00 an hour.


推荐答案

我将在初始化中包括EmployeeClass的参数ProductionWorker的方法参数传递给超类。

I would include the parameters of the EmployeeClass in the init method parameters of the ProductionWorker to pass along to the superclass.

对于python 3,您可以执行super().__ init ___()而不是EmployeeClass .__ init __()。

For python 3 you can do super().__init___() rather than EmployeeClass.__init__().

此外,您应该考虑使用描述符而不是实现getter和setter,因为这是实现此目的的Python方法。

Additionally you should consider using descriptors rather than implementing getters and setters as that is the pythonic way to do that.

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

这篇关于如何更改子类内部的superClass属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆