__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.
ゲッターとセッターは使用しないでください。必要に応じてプロパティを使用します。 double_underscoresは使用しないでください。 'set_xxx'属性を' __init__'にセットするのは、あなたが意図するものではありません。 – Daniel
チップをありがとう。私はゲッター、セッター、ダブルアンダースコアを使うように教えられました。 –