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.
Latihan ini adalah bagian dari kursus
Introduction to Object-Oriented Programming in Python
Petunjuk latihan
- Add a
give_raise()method toManagerthat accepts the same parameters asEmployee.give_raise(), plus abonusargument with the default value of1.05(bonus of 5%). - Within the method, calculate
new_amountby multiplyingamountbybonus - Within the method, use the
Employee's method to raise salary bynew_amount.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
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)