Aan de slagGa gratis aan de slag

Method inheritance

In this exercise, you'll extend the Manager class (which is inherited from the Employee class), created in the previous exercise, by creating a method called give_raise(). It will be similar to the method of the same name in the Employee class, but include an additional argument called bonus.

The Manager class you previously created has been provided for you in script.py.

Deze oefening maakt deel uit van de cursus

Introduction to Object-Oriented Programming in Python

Cursus bekijken

Oefeninstructies

  • Add a give_raise() method to Manager that accepts the same parameters as Employee.give_raise(), plus a bonus argument with the default value of 1.05 (bonus of 5%).
  • Within the method, calculate new_amount by multiplying amount by bonus
  • Within the method, use the Employee's method to raise salary by new_amount.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

class Employee:
  def __init__(self, name, salary=30000):
    self.name = name
    self.salary = salary

  def give_raise(self, amount):
    self.salary += amount

class Manager(Employee):
  def display(self):
    print("Manager ", self.name)

  def __init__(self, name, salary=50000, project=None):
    Employee.__init__(self, name, salary)
    self.project = project

  # Add a give_raise method
  ____:
    ____
    ____
    
mngr = Manager("Ashta Dunbar", 78500)
mngr.give_raise(2000, bonus=1.03)
print(mngr.salary)
Code bewerken en uitvoeren