13. Override


#1

This is my code:

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

# Add your code below!
class PartTimeEmployee(Employee):
  
  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 12
  
emp = Employee('Jake')
pt_employee = PartTimeEmployee('Lawrence')

print emp.calculate_wage(10)
print pt_employee.calculate_wage(10)

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-classes/exercises/override?action=lesson_resume

It is correct but I don’t fully understand why do we write: “self.hours = hours”… what is the reason?

Thanks!!

<do not remove the three backticks above>

#2

This is so that the hours will be updated dynamically for the each object without interfering with another object’s value.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.