2010-09-16 13 views
48

क्या कोई मुझे वास्तविक जीवन, पॉलिमॉर्फिज्म का व्यावहारिक उदाहरण दे सकता है? मेरा प्रोफेसर मुझे वही पुरानी कहानी बताता है जिसे मैंने हमेशा + ऑपरेटर के बारे में सुना है। a+b = c और 2+2 = 4, तो यह polymorphism है। मैं वास्तव में ऐसी परिभाषा से खुद को जोड़ नहीं सकता, क्योंकि मैंने इसे कई पुस्तकों में पढ़ और पढ़ा है।पॉलिमॉर्फिज्म का व्यावहारिक उदाहरण

मुझे जो चाहिए वह कोड के साथ वास्तविक दुनिया का उदाहरण है, जिसे मैं वास्तव में जोड़ सकता हूं।

उदाहरण के लिए, यहां एक छोटा उदाहरण है, बस अगर आप इसे विस्तारित करना चाहते हैं।

>>> class Person(object): 
    def __init__(self, name): 
     self.name = name 

>>> class Student(Person): 
    def __init__(self, name, age): 
     super(Student, self).__init__(name) 
     self.age = age 
+0

आप विशेष रूप से ऑपरेटर बहुरूपता (भी बुलाया ऑपरेटर ओवरलोडिंग), या [बहुरूपता] (http://en.wikipedia.org के बारे में पूछ रहे हैं,/विकी/Type_polymorphism) सामान्य रूप से? सामान्य रूप से –

+0

पॉलिमॉर्फिज्म। – Maxx

उत्तर

146

चेक विकिपीडिया उदाहरण: यह एक उच्च स्तर पर बहुत उपयोगी है:

class Animal: 
    def __init__(self, name): # Constructor of the class 
     self.name = name 
    def talk(self):    # Abstract method, defined by convention only 
     raise NotImplementedError("Subclass must implement abstract method") 

class Cat(Animal): 
    def talk(self): 
     return 'Meow!' 

class Dog(Animal): 
    def talk(self): 
     return 'Woof! Woof!' 

animals = [Cat('Missy'), 
      Cat('Mr. Mistoffelees'), 
      Dog('Lassie')] 

for animal in animals: 
    print animal.name + ': ' + animal.talk() 

# prints the following: 
# 
# Missy: Meow! 
# Mr. Mistoffelees: Meow! 
# Lassie: Woof! Woof! 

सूचना निम्नलिखित: सभी जानवरों "बात", लेकिन वे अलग तरह से बात करते हैं। इस प्रकार "टॉक" व्यवहार इस तरह के पॉलीमोर्फिक है कि यह जानवर पर निर्भर करता है। तो, अमूर्त "पशु" अवधारणा वास्तव में "बात" नहीं करती है, लेकिन विशिष्ट जानवरों (जैसे कुत्तों और बिल्लियों) के पास "टॉक" कार्रवाई का ठोस कार्यान्वयन होता है।

इसी प्रकार, "एड" ऑपरेशन कई गणितीय इकाइयों में परिभाषित किया गया है, लेकिन विशेष रूप से आप विशिष्ट नियमों के अनुसार "जोड़ते हैं": 1 + 1 = 2, लेकिन (1 + 2i) + (2-9i) = (3-7i)।

पॉलिमॉर्फिक व्यवहार आपको "अमूर्त" स्तर में सामान्य तरीकों को निर्दिष्ट करने की अनुमति देता है, और उन्हें विशेष उदाहरणों में लागू करता है।

अपने उदाहरण के लिए:

class Person(object): 
    def pay_bill(): 
     raise NotImplementedError 

class Millionare(Person): 
    def pay_bill(): 
     print "Here you go! Keep the change!" 

class GradStudent(Person): 
    def pay_bill(): 
     print "Can I owe you ten bucks or do the dishes?" 

तुम देखो, millionares और स्नातक छात्रों दोनों व्यक्तियों रहे हैं। लेकिन जब बिल का भुगतान करने की बात आती है, तो उनका विशिष्ट "बिल का भुगतान" कार्रवाई अलग होती है।

+1

जिसमें 'अमूर्त' विधि है, 'NotImplementedError' ब्रेक' सुपर 'बढ़ाएं। – aaronasterling

+1

यह विरासत से अलग कैसे होता है, जिससे उप-वर्ग माता-पिता वर्ग में विधियों को ओवरराइड करते हैं? – Pyderman

+1

@ पाइडरमैन विरासत पॉलिमॉर्फिज्म प्राप्त करने के लिए ** साधन ** में से एक है। – Escualo

10

पायथन में एक आम वास्तविक उदाहरण file-like objects है। वास्तविक फ़ाइलों के अलावा, StringIO और BytesIO सहित कई अन्य प्रकार, फ़ाइल-जैसी हैं। फाइल के रूप में कार्य करने वाली एक विधि उन पर भी कार्य कर सकती है क्योंकि वे आवश्यक विधियों का समर्थन करते हैं (उदा। read, write)।

5

एक C++ ऊपर जवाब से बहुरूपता का उदाहरण होगा:

class Animal { 
public: 
    Animal(const std::string& name) : name_(name) {} 
    virtual ~Animal() {} 

    virtual std::string talk() = 0; 
    std::string name_; 
}; 

class Dog : public Animal { 
public: 
    virtual std::string talk() { return "woof!"; } 
}; 

class Cat : public Animal { 
public: 
    virtual std::string talk() { return "meow!"; } 
}; 

void main() { 

    Cat c("Miffy"); 
    Dog d("Spot"); 

    // This shows typical inheritance and basic polymorphism, as the objects are typed by definition and cannot change types at runtime. 
    printf("%s says %s\n", c.name_.c_str(), c.talk().c_str()); 
    printf("%s says %s\n", d.name_.c_str(), d.talk().c_str()); 

    Animal* c2 = new Cat("Miffy"); // polymorph this animal pointer into a cat! 
    Animal* d2 = new Dog("Spot"); // or a dog! 

    // This shows full polymorphism as the types are only known at runtime, 
    // and the execution of the "talk" function has to be determined by 
    // the runtime type, not by the type definition, and can actually change 
    // depending on runtime factors (user choice, for example). 
    printf("%s says %s\n", c2->name_.c_str(), c2->talk().c_str()); 
    printf("%s says %s\n", d2->name_.c_str(), d2->talk().c_str()); 

    // This will not compile as Animal cannot be instanced with an undefined function 
    Animal c; 
    Animal* c = new Animal("amby"); 

    // This is fine, however 
    Animal* a; // hasn't been polymorphed yet, so okay. 

} 
संबंधित मुद्दे