"" " Inherited polymorphism " "" __author__ = ' shaozhiqi ' # ----------------- --------------- parent --------- class Animal (Object): DEF RUN (Self): Print ( ' Animal IS running ... ' ) # ----------------- subclass ------------------------ class Dog (Animal): Pass # ---------------- - subclass ---------------------------------------- class Cat (Animal): Pass # ------------------- subclass instance -------------------------- ----------- Dog = Dog () dog.run () # Animal is running... cat = Cat() cat.run() # Animal is running... # --------------------重新定义dog 有自己的run方法------------------- class Dog(Animal): def run(self): print('Dog is running...') def eat(self): print('Dog eating meat...') dog = Dog() dog.run() # Dog is running... dog.eat() # Dog eating meat... #When the parent class and subclass there the same run () method, we say, sub-class run () covering the run parent class (), when the code is running, always calls the run subclass () . # In this way, we get another benefit of inheritance: polymorphism. # Subclass, the type of data that it can also be seen as a parent. However, in turn, will not do: Print (isinstance (Dog, Dog)) # True Print (isinstance (Dog, Animal)) # True A = Animal () Print (isinstance (A, Dog)) # False # ---- -------------------------adaptation----------------------- ----------- DEF run_selfrun (Animal): animal.run () run_selfrun (A) # Animal IS running ... run_selfrun (Dog) # Dog IS running ... #Animal a new subclass, run_selfrun () does not require any modification, this method can extract business upward, # fact, any dependency or as a function of Animal method parameters are used without modification to normal operation the reason is that polymorphic. # Closing principle: # extended open: Animal allow new subclasses; # closed for modification: Animal no modification dependent type run_selfrun () function and the like.