मल्टीलेवल इनहेरिटेंस
आपने सिंगल और मल्टिपल इनहेरिटेंस दोनों करके हाथ आज़माए हैं। इस अभ्यास में, आप 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)
यह अभ्यास पाठ्यक्रम का हिस्सा है
Intermediate Object-Oriented Programming in Python
अभ्यास निर्देश
Tabletसे इनहेरिट करने वालीSmartphoneक्लास परिभाषित करें, पैरेंट कंस्ट्रक्टर कॉल करें, औरphone_numberइंस्टेंस-लेवल एट्रिब्यूट सेट करें.- एक
send_textमेथड बनाएँ, जोSmartphoneकेphone_numberसेrecipientको टेक्स्टmessageभेजे. 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")