2013-10-05 85 views
0

它適用於Employeecalculate_wage,但在嘗試創建PartTimeEmployee的實例並調用PartTimeEmployee的父類的calculate_wage方法時返回錯誤。爲什麼下面的代碼導致錯誤?

class Employee(object): 
    """Models real-life employees!""" 
    def __init__(self, employee_name): 
     self.employee_name = employee_name 

    def calculate_wage(self, hours): 
     self.hours = hours 
     return hours * 20.00 

class PartTimeEmployee(Employee): 
    def __init__(self, employee_name): 
     self.employee_name = employee_name 
    def calculate_wage(self, hours): 
     self.hours = hours 
     return hours * 12.00 
    def full_time_wage(self, hours): 
     return super(PartTimeEmployee, self).calculate_wage(self, hours) 

milton = PartTimeEmployee("Milton") 
print (milton.full_time_wage(10)) 
+1

「但返回錯誤」...唉,錯誤消息似乎是一個祕密.... – glglgl

+0

你能請我選擇我的答案是正確的嗎? –

回答

5
return super(PartTimeEmployee, self).calculate_wage(self, hours) 

是不正確,應該是

return super(PartTimeEmployee, self).calculate_wage(hours) 

而且下一次:還張貼您看到的錯誤消息。

+0

爲什麼不需要'自我'?它在Employee類中定義爲calculate_wage(self,hours),我認爲我應該複製粘貼它。 – user2742080

+0

因爲'self'是從方法調用前的內容派生的(當你在執行'foo.calculate_wage(hours)''時,'self'被設置爲'foo') –

+0

如果是這樣的話,標記我的答案是正確的? –

相關問題