Herança de métodos
Inheritance is powerful because it allows us to reuse and customize code without rewriting existing code. By calling methods of the parent class within the child class, we reuse all the code in those methods, making our code concise and manageable.
Neste exercício, você continuará a trabalhar com a classe Manager
, que é herdada da classe Employee
. Você adicionará novos dados à classe e personalizará o método give_raise()
do Capítulo 1 para aumentar o valor do aumento do gerente em uma porcentagem de bônus sempre que ele receber um aumento.
Uma versão simplificada da classe Employee
, bem como o início da classe Manager
da lição anterior, é fornecida para você no painel de script.
Este exercício faz parte do curso
Programação orientada a objetos em Python
Exercício interativo prático
Experimente este exercício preenchendo este código de exemplo.
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):
# Add a constructor
def __init__(self, name, ____, ____):
# Call the parent's constructor
____.____(____, ____, salary)
# Assign project attribute
____
def display(self):
print("Manager ", self.name)