처음부터 클래스 만들기
이 장의 마지막 연습 문제에서는 Python 개발자가 되어 생성자를 포함한 나만의 클래스를 처음부터 만들어 보겠습니다!
두 값을 더하고, 빼고, 곱할 수 있는 메서드를 포함한 Calculator 클래스를 설계하세요.
이 연습은 강의의 일부입니다
Python 객체 지향 프로그래밍 입문
연습 안내
- 초기화 시 두 매개변수
num_one과num_two를 받는Calculator클래스를 정의하세요. self.num_one에num_two를 더한 값을 반환하는addition()메서드를 정의하세요.self.num_one에서num_two를 뺀 값을 반환하는subtraction()메서드를 정의하세요.self.num_one에num_two를 곱한 값을 반환하는multiplication()메서드를 정의하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Define and initialize the Calculator class
____:
____ ____(____, ____, ____):
self.____ = ____
self.____ = ____
# Create the addition method
def ____(____):
return self.____ ____ self.____
# Create the subtraction method
def ____(____):
return ____
# Create the multiplication method
def ____(____):
____