super() के साथ कार्यक्षमता का विस्तार

Python में, super() एक बिल्ट-इन फ़ंक्शन है जिसका उपयोग पैरेंट क्लास से किसी मेथड को कॉल करने के लिए किया जाता है। यह क्लास इनहेरिटेंस में विशेष रूप से उपयोगी होता है जब आप सबक्लास में सुपरक्लास के किसी मेथड के व्यवहार को विस्तारित या संशोधित करना चाहते हैं। super() सबक्लास की सही प्रारंभिककरण (initialization) के लिए भी महत्वपूर्ण है।
यह समझने के लिए कि super() को मेथड्स और क्लास initialization के साथ कैसे उपयोग किया जा सकता है, आइए जानवरों की आवाज़ों का उदाहरण लेते हैं। हम एक Animal क्लास और दो सबक्लास, Dog और Cat बनाएंगे।
class Animal:                       # Define a class named 'Animal'
    def __init__(self, species):
        self.species = species

    def speak(self):
        return 'Sounds...'

class Dog(Animal):                  # Define a subclass named 'Dog' that inherits from 'Animal'
    def __init__(self, name):
        super().__init__('Dog')     # Call the parent's '__init__' method using 'super()'
        self.name = name            # Additional attribute 'name' for 'Dog'
        print(self.species)         # Will print Dog

    def speak(self):
        original = super().speak()  # Call the parent's 'speak' method using 'super()'
        return original + ' Woof!'

class Cat(Animal):                  # Define a subclass named 'Cat' that inherits from 'Animal'
    def __init__(self, name):
        super().__init__('Cat')     # Call the parent's '__init__' method using 'super()'
        self.name = name            # Additional attribute 'name' for 'Cat'

    def speak(self):
        original = super().speak()  # Call the parent's 'speak' method using 'super()'
        return original + ' Meow!'

dog = Dog('Rex')                              # Create an instance of the 'Dog' class
cat = Cat('Fluffy')                           # Create an instance of the 'Cat' class
print(dog.name, dog.species, dog.speak())     # Rex Dog Sounds... Woof!
print(cat.name, cat.species, cat.speak())     # Fluffy Cat Sounds... Meow!
यहाँ, Dog और Cat दोनों सबक्लास अपने सुपरक्लास Animal के __init__() मेथड को कॉल करने के लिए super().__init__() का उपयोग करते हैं। इससे सुनिश्चित होता है कि Dog या Cat का प्रत्येक उदाहरण सही प्रजाति नाम के साथ प्रारंभिक (initialize) किया गया है। इसी तरह, दोनों सबक्लास में speak() मेथड Animal के speak() मेथड को कॉल करने के लिए super().speak() का उपयोग करते हैं, जिससे वे अपने स्वयं के speak() मेथड में मूल जानवर की आवाज़ शामिल कर सकें।
इस प्रकार, super() फ़ंक्शन सबक्लास को अपने सुपरक्लास के व्यवहार का लाभ उठाने और विस्तारित करने की अनुमति देता है। यह कोड की पुन: उपयोगिता को बढ़ाने, सबक्लास की सही प्रारंभिककरण सुनिश्चित करने, और आपके कोड को मॉड्यूलर और पठनीय रखने में महत्वपूर्ण भूमिका निभाता है। अंततः, यह Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग के लिए एक शक्तिशाली उपकरण बन जाता है।

चुनौती: वाहन क्लासेज

आपको विभिन्न प्रकार के वाहनों के लिए क्लासेज बनाने का कार्य दिया गया है। इन क्लासेज को वास्तविक दुनिया के वाहनों के कुछ गुणों और व्यवहारों को सिमुलेट करना चाहिए।
  1. Vehicle क्लास: बेस क्लास। इसमें निम्नलिखित विशेषताएं होनी चाहिए:
      • make: represents the manufacturer of the vehicle.
      • model: represents the specific model of the vehicle.
      • year: represents the year the vehicle was manufactured.
      • A method named honk() that, when called, prints the string Honk!.
  1. Car क्लास: यह क्लास Vehicle क्लास से इनहेरिट करेगी। इसमें Vehicle क्लास की सभी विशेषताएं और मेथड होंगे, साथ ही निम्नलिखित:
      • color: represents the color of the car.
      • It should override the honk() method of the Vehicle class. When called, this method should print the string Beep beep!.
  1. Bicycle क्लास: यह क्लास भी Vehicle क्लास से इनहेरिट करेगी। इसमें Vehicle क्लास की सभी विशेषताएं और मेथड होंगे, लेकिन इसे honk() मेथड को ओवरराइड करना होगा। कॉल करने पर, यह मेथड Ring ring! प्रिंट करेगा।
आपका कार्य इन क्लासेज और मेथड्स को लागू करना है।
इनपुट
आउटपुट
car=Car('Tesla', 'Model S', 2022, 'Red'); bicycle=Bicycle('Schwinn', 'Fastback', 2020); car.honk(); bicycle.honk()
Beep beep! Ring ring!
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue