2017-11-09 11 views
0

__init__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." 

私の主:

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. 
+0

ゲッターとセッターは使用しないでください。必要に応じてプロパティを使用します。 double_underscoresは使用しないでください。 'set_xxx'属性を' __init__'にセットするのは、あなたが意図するものではありません。 – Daniel

+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

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) 

これは私が得る結果であり、 EmployeeClasのパラメータを含めますsをスーパークラスに渡すProductionWorkerのinitメソッドのパラメータに追加します。

の場合、EmployeeClass .__ init __()ではなく、super().__ init ___()を実行できます。

さらに、ゲッターとセッターを実装するのではなく、descriptorsを使用することをお勧めします。

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 
+0

ここにdocsへのリンクがあります:https://docs.python.org/3/reference/datamodel.html#object.__init__ –

+0

これを行うと、自分の名前と番号が「未解決の参照」として認識されています –

関連する問題