다단계 상속
이제 단일 상속과 다중 상속을 모두 실습해 보셨네요. 이번 연습에서는 다단계 상속을 구현해 새로운 버전의 Smartphone 클래스를 만들어 볼 거예요.
시작을 돕기 위해 아래에 Computer와 Tablet 클래스를 정의해 두었습니다. Tablet이 Computer 클래스를 상속한다는 점이 중요해요. 화이팅!
class Computer:
def __init__(self, brand):
self.brand = brand
def browse_internet(self):
print(f"Using {self.brand}'s default internet browser.")
class Tablet(Computer):
def __init__(self, brand, apps):
Computer.__init__(self, brand)
self.apps = apps
def uninstall_app(self, app):
if app in self.apps:
self.apps.remove(app)
이 연습은 강의의 일부입니다
Python 중급 객체 지향 프로그래밍
연습 안내
Tablet을 상속하는Smartphone클래스를 정의하고, 부모 생성자를 호출한 뒤phone_number인스턴스 속성을 정의하세요.Smartphone의phone_number에서recipient에게 텍스트message를 보내는send_text메서드를 만드세요.personal_phone이라는 이름의Smartphone객체를 생성하고.browse_internet()메서드를 호출하세요. 그리고Weather앱을 삭제한 다음, Chuck에게 문자로Time for a new mission!메시지를 보내세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Create a Smartphone class that inherits from Tablet
class ____(____):
def __init__(self, brand, apps, phone_number):
____.____(self, ____, ____)
self.phone_number = ____
# Create send_text to send a message to a recipient
def ____(self, message, recipient):
print(f"Sending {____} to {____} from {____.____}")
# Create an instance of Smartphone, call methods in each class
____ = Smartphone("Macrosung", ["Weather", "Camera"], "801-932-7629")
personal_phone.____()
personal_phone.____("____")
personal_phone.____("____", "Chuck")