시작하기무료로 시작하기

메서드 상속

이 연습 문제에서는 이전 문제에서 만든 Employee 클래스를 상속받는 Manager 클래스를 확장해 give_raise()라는 메서드를 추가해 보세요. 이 메서드는 Employee 클래스의 동명 메서드와 유사하지만, bonus라는 추가 인수를 포함해요.

이전에 만든 Manager 클래스는 script.py에 제공되어 있어요.

이 연습은 강의의 일부입니다

Python 객체 지향 프로그래밍 입문

강의 보기

연습 안내

  • Managergive_raise() 메서드를 추가하고, Employee.give_raise()와 동일한 매개변수에 더해 기본값이 1.05(5% 보너스)인 bonus 인수를 받도록 하세요.
  • 메서드 안에서 amountbonus를 곱해 new_amount를 계산하세요.
  • 메서드 안에서 Employee의 메서드를 사용해 급여를 new_amount만큼 인상하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

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)
코드 편집 및 실행