1.1 Inheritance and polymorphism1.1.1 Inheritance
when we define a class , we can inherit from an existing class, the new class is called a subclass (subclass), and the inherited class called a base class, parent class, or superclass ( Base class,Super class).
>>> class Animal (object):
... def run (self):
... print (' Animal is running ... ') # parent class
...
>>> class Dog (Animal):
... pass # sub- class
...
>>> class Cat (Animal):
... pass # sub- class
...
>>> dog = Dog ()
>>> Dog.run ()
Animal is running ...
>>> cat = Cat ()
>>> Cat.run ()
Animal is running ...
Inheritance means that the subclass obtains the full functionality of the parent class.
The second benefit of inheritance requires a little improvement in our code.
The Dog class is modified as follows
>>> class Dog (Animal):
... def run (self):
... print (' Dog is running ')-- Modify part
...
>>> dog = Dog ()
>>> Dog.run ()
Dog is running
when both the subclass and the parent class have the same run () method, we say that the subclass's run () overrides the parent class's run (), The run () of the subclass is always called when the code is running.
1.1.2 polymorphic
>>> a = list () #list type
>>> B = Animal () #Animal type
>>> C = Dog () #Dog type
>>> isinstance (A, list)
True
>>> Isinstance (b, Animal)
True
>>> isinstance (c, Dog)
True
>>> isinstance (c, Animal)
True
>>> Isinstance (b, Dog)
False
from the top, it is found that B is only Animal type cannot be a dog type;c is both a Animal type and a dog type.
In an inheritance relationship, if the data type of an instance is a subclass, its data type can also be considered a parent class. But, in turn, it doesn't work.
Understanding of polymorphic Benefits
>>> class Animal (object):
... def run (self):
... print (' Animal is running ... ')
...
>>> class Dog (Animal):
... def run (self):
... print (' Dog is running ... ')
...
>>> def run_twice (x):
... X.run ()
... X.run ()
...
>>> Run_twice (Animal ())-- equivalent to B = Animal () instantiation,Run_twice (b)
Animal is running ...
Animal is running ...
>>> Run_twice (Dog ())
Dog is running ...
Dog is running ...
>>> # When we add a tortoise , we don't need to change the data type
...
>>> Class Tortoise (Animal):
... def run (self):
... print (' tortoise is running slowly ... ')
...
>>> Run_twice (Tortoise ())
Tortoise is running slowly ...
Tortoise is running slowly ...
New The Tortoise subclass (understood as the Animal type) does not need to modify the run_twice function data type .
in fact, any reliance Animal functions or methods as parameters can be run without modification , the reason is polymorphism.
The advantage of polymorphism is that when we need to pass inDog,Cat,Tortoise... , we only need to receive AnimaltypeIt is possible, becauseDog,Cat,Tortoise... 's All Animaltype, and then, follow Animaltype to operate. BecauseAnimaltype hasRun ()method, so that any type passed in, as long as it isAnimalclass or subclass, the actual type is automatically calledRun ()method, which is the meaning of polymorphism:
for a variable, we just need to know it is animal , You can safely call run () run () is! Font-family: ' The song Body '; The method is the function in animal dog cat< Span style= "font-family: ' The song Body '; > or tortoise animal run () method written correctly , without having to control how the original code is called by the
open for extensions: allow new Animal sub-class ;
Closed for modification: No need to modify dependencies Animal type of run_twice () and other functions .
Here's a good understanding: why " Object class , which is the class that all classes will eventually inherit ,the object class is the topmost class.
1.1.3 Static LanguageVsDynamic Language
for static languages, such as Java), if you need to pass in the Animal type, the incoming object must be a Animal type or its subclass, otherwise the run () method cannot be called .
for for dynamic languages like Python, you don't necessarily need to pass in the Animal type. We just need to make sure that the incoming object has a run () method.
in understanding, you can understand a similar concept, an object with the same method, and the following is the object with the Run method.
>>> class Animal (object):
... def run (self):
... print (' Animal is running ... ')
...
>>> class Dog (object):
... def run (self):
... print (' Dog is running ... ')
...
>>> def run_twice (x):
... X.run ()
... X.run ()
...
>>> Run_twice (Animal ())
Animal is running ...
Animal is running ...
>>> Run_twice (Dog ())
Dog is running ...
Dog is running ...
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1826205
Python Object-oriented programming--inheritance and polymorphism