Python | polymorphism

So far not very understanding, first understand the existing record, at a later date during programming a deeper and more correct understanding, correct again.

This article documents heard three polymorphisms in the learning process, "the first argument," such as the use of the following classification is to facilitate the recording of purely personal classification.

table of Contents:

First, I have seen the explanation of the three polymorphic

Second, the duck model

 

-----text-----

First, I have seen the explanation of the three polymorphic

1. Different subclass object, call the parent class the same method, generating different results of execution

 Objective: To increase the flexibility of the code

 To inherit and override the parent class method as a precondition

 Method is invoked skills, it will not affect the internal design of the class

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

    def play(self):
        print("%s jump jump play" % self.name)


class XiaotianDog(Dog):
    def play(self):
        print("%s fly fly play" % self.name)


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

    def play_with_dog(self, dog):
        print( " % S% S and Play Happy Happy " % (the self.name, dog.name)) 
        dog.play ()   # different objects, call the same method, different results 


wangcai = Dog ( " wangcai " ) 
Dawang XiaotianDog = ( " Dawang " ) 

xiaoming = the Person ( " xiaoming " ) 

# xiaoming.play_with_dog ( "wangcai") error friends 
xiaoming.play_with_dog (wangcai)   # to pass in a dog objects 
xiaoming.play_with_dog (dawang)


2. The object has a variety of forms, with different functions in different forms demonstrate its environment, the object having said characteristic polymorphic
Polymorphism occurs in [inheritance] relationship footing
 1 class Teacher():
 2     def teach(self):
 3         print("teach---")
 4 
 5 
 6 class Driver():
 7     def drive(self):
 8         print("drive---")
 9 
10 
11 class Man(Teacher, Driver):
12     def teach(self):
13         print("teach python")
14 
15     def drive(self):
16         Print ( " Drive CAR " )
 . 17  
18 is  
. 19  class Demo ():
 20 is      DEF Travel (Self, Driver):
 21 is          driver.drive ()
 22 is  
23 is      DEF Study (Self, Teacher):
 24          teacher.teach ()
 25  
26 is  
27  # create a driver object is to complete the trip by car it 
28 d = driver ()
 29 demo1 = Demo ()
 30  demo1.travel (d)
 31  
32  # directly to a target drive will be done to drive this thing, does not require Create a separate driver polymorphic objects [method] 
33  #This will drive the object may also be other, no matter, as long as the drive just fine, said so much, the road is not? Ha ha 
34 is man = Man ()
 35 Demo = Demo ()
 36  # man.drive () 
37 [  demo.travel (man)
 38 is  
39  # in accordance with the idea of polymorphism, if there is a need to place a Teacher, this may be directly transmitted man 
40 demo.study (man)

 


3. The ability to provide for different interfaces of the basic mode (data type)
Interface: refers to a function or method

Such as the print function to a string, integer, and so the list of different data types provide an interface, simply use a print can support all data types, 
without the need to define and call different functions, such as: print_str, print_int, print_list etc.
1 print("hello")
2 print(23)
3 print([1, 2, 3])
 
Suppose you want to write a program that creates cats, dogs, sheep three animals, corresponding to eat fish, bones, grass, you can define the Cat / Dog / Sheep three classes, eat and methods of their definition. 
Cat.eat () corresponding to the cat to eat, Dog.eat () corresponds to the dogs, Sheep.eat () corresponding to the sheep eat, so eat each object has a method interface to support the animal eats.
Namely: Provides interface for the same three different objects.
If no polymorphism is required corresponding to a respective unique to each animal cat_eat () / dog_eat () / sheep_eat.
1  class Dog:
 2      DEF EAT (Self):
 3          Print ( " I'm a dog, I eat the bones " )
 4  
5  
6  class Cat:
 7      DEF EAT (Self):
 8          Print ( " I Am a Cat, I eat fish " )
 . 9  
10  
. 11  class sheep:
 12 is      DEF EAT (Self):
 13 is          Print ( " I sheep grazing I " )
 14  
15  
16  DEF animal_eat (Animal):
 . 17      animal.eat ()  # Different animals (data objects) using the same interface 
18  
19  
20  animal_eat (Dog ())
 21  animal_eat (Cat ())
 22  animal_eat (Sheep ())
 23  "" " 
24-  code execution, print:
 25  I am a dog I eat bones
 26  I'm a cat, I eat fish
 27  I am a sheep, grazing my
 28  "" "

 

 

Second, the duck model

 

Is called class does not use inheritance, but the syntax level to meet the call relations 
special polymorphic forms

not only the methods, properties can also have duck type, such as, you need a name attribute that to a band so the property can be the

polymorphism: in form and calls meet the requirements of the so-called polymorphic
duck model: meet on the call relationship, but does not meet the formal
1  # Requirements: to find a man came to teach a certain thing 
2  class Teacher ():
 3      DEF the Teach (Self):
 4          Print ( " the Teach --- " )
 5  
6  
7  class Man (Teacher):
 8      # DEF the Teach (Self ): 
. 9      #      Print ( "Teach Python") 
10  
. 11      DEF Drive (Self):
 12 is          Print ( " Drive CAR " )
 13 is  
14  
15  class Demo ():
 16      DEF Study (Self, Teacher):
 . 17         teacher.teach ()
 18  
19  
20  class TeachGame ():
 21      DEF the Teach (Self):
 22          Print ( " teach to play the game " )
 23  
24-  
25  # to find a Teacher inherit the parent class man teach methods can come and teach something 
26 is D = Demo ()
 27 man = man ()
 28  d.study (man)
 29  
30  # TeachGame inheritance class itself does not involve, but teach methods Dome instance of an object d, to be performed in the study of the process just TeachGame there are 
31  # false when it is a special form of polymorphism of 
32 Player = TeachGame ()
 33 is d.study (Player)

 

 

Currently is a silly you could not tell.

[This article is only for learning exchanges]

 

Guess you like

Origin www.cnblogs.com/ykit/p/11250378.html