How to understand polymorphism in Python? (Reprint, so I understand.) [Plus] self-perception

Sticky write some of my own views, according to the wording basis Python tutorial: Polymorphism means the ability of the same treatment of different types and classes of objects, both methods can be called without knowing it is an object belongs to which class.

.Clear method is like, you can give a list of the dictionary calls this method.

To give .count strings, lists, tuples, can be used, this method is polymorphic,

You did not find, can be operated with the different types of the same function. Parity doubt that, without knowing which class is the object that sentence, in fact, these methods do we know what kind of,

I do not know what kind, really can not mess with, I think the literal meaning of which class do not need to know that these methods may be within the range of their type of operation, does not need to know which kind of meaning.

For example, you need to know is in front of .count list, Ganso or string, came on the last method is like, do not need to type isinstance or judgment, will make the program easier to look at the past, refreshing

 

The above is my understanding.

 


Python in the role of polymorphic

make functions with different functions can use the same function name, so that you can call different content (function) function with a function name.
Python polymorphic features of

an instance object is only concerned with a method of the same name, is not concerned with the type of object belongs;
2, between the class object belongs, inheritance dispensable;
3, may increase the benefits of multi-state code external calls flexibility, make the code more versatile, relatively strong compatibility;
4, polymorphism is a technique called method will not affect the interior design classes.
Scenarios polymorphic

no inheritance relationship between classes 1. object belongs
call the same function fly (), passing different parameters (objects), you can achieve different functions

class Duck (object): # Duck class
    def fly ( Self):
        Print ( "duck flew along the ground up")

class Swan (Object): # Swan class
    def fly (Self):
        Print ( "swan flying in the air")

class Plane (Object): # airplane class
    def fly (Self):
        Print ( "airplane took off rumble")

def fly (obj): # function to achieve functional fly
    obj.fly ()

Duck Duck = ()
Fly (Duck)

Swan Swan = ()
Fly (Swan)

Plane Plane = ()
Fly (Plane)

=== operating results: ================================================== =================================
duck flew along the ground up
swans flying in the air
the plane took off rumble

    1
    2
    . 3
    . 4
    . 5
    . 6
    . 7
    . 8
    . 9
    10
    . 11
    12 is
    13 is
    14
    15
    16
    . 17
    18 is
    . 19
    20 is
    21 is
    22 is
    23 is
    24
    25
    26 is
    27
    28

inheritance relationships between classes 2. The object belongs (wider application)

class gradapa (Object):
    DEF the __init __ (Self, Money):
        self.money = Money
    DEF P (Self):
        Print ( "the this gradapa IS ")
 
class Father (gradapa):
    DEF __init __ (Self, Money, the Job):
        Super () .__ the init __ (Money)
        self.job = the Job
    DEF the p-(Self):
        Print (" the this IS Father, I rewrote parent class method ")
 
class Mother (gradapa):
    DEF __init __ (Self, Money, the Job):
        Super () .__ the init __ (Money)
        self.job = the Job
 
    DEF the p-(Self):
         Print (" the this iS Mother, my weight write a parent's method ")
         return 100
         
# Define a function, the function call class p () method
DEF fc (obj):
    obj.p ()
gradapa1 = gradapa (3000)
father1 = Father (2000, "workers")
mother1 = Mother (1000, "teacher" )

FC (# polymorphism embodied here gradapa1) function is the same, after passing different parameters, different functions can be achieved.
FC (father1)
Print (FC (mother1))
=== result: ===== ================================================== ============================
the this iS gradapa
the this iS father, I rewrote the parent class
this is mother, I rewrite a parent method
100

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7
    . 8
    . 9
    10
    . 11
    12 is
    13 is
    14
    15
    16
    . 17
    18 is
    . 19
    20 is
    21 is
    22 is
    23 is
    24
    25
    26 is
    27
    28
    29
    30
    31 is
    32
    33 is
    34 is
    35
    36
    37 [

 
----------------
Copyright: This is CSDN bloggers "tigerlib" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/weixin_44695969/article/details/92175840

Guess you like

Origin www.cnblogs.com/sidianok/p/11784947.html