I. Polymorphism and polymorphism
Polymorphism refers to a variety of things in a class, such as animals in many forms: cats, dogs, and pigs.
class Animal: #The same kind of things: animal def talk (self): pass class Cat (Animal): #One of the animal forms: cat def talk (self): print ( ' 喵 喵 喵' ) class Dog (Animal): # shape of two animals: dogs DEF Talk (Self): Print ( ' bark Wang ' ) class pig (animal): # shape of three animals: pigs DEF Talk (Self): Print ( ' hum hum ' )
Polymorphism refers to that you can use the object directly without considering the specific type of the object. This requires the design of the object to be unified into one method: for example, cat, dog, pig are all animals, but all animals There must be a talk method, so we can directly use them without considering what kind of animal they are.
>>> cat.talk () meow meow meow >>> dog.talk () Wangwangwang >>> pig.talk () hum
Further, we can define a unified interface to use.
>>> def Talk (animal): ... animal.talk () ... >>> Talk (cat) meow meow >>> Talk (dog) 汪汪 汪 >>> Talk (pig) hum
Everything in Python is an object, which itself supports polymorphism.
#We can directly count the length of the three objects without considering the three types . __Len__ () l. __Len__ () t. __Len__ () # Python has a unified interface len (s) len (l ) len (t)
The advantage of polymorphism is to enhance the flexibility and extensibility of the program. For example, a new class is created by inheriting the Animal class. The object obj instantiated can be used in the same way as obj.talk ()
>>> class Wolf (Animal): #Another form of animal: wolf ... def talk (self): ... print ('嗷 ...') ... >>> wolf = Wolf () # Instance 出 一头狼>>>> wolf.talk () # The user does not need to care about what type of wolf is and calls talk aw ...
In summary, we learned that the essence of polymorphism is that different classes have the same method name, so that we can use objects in a way regardless of the class, and we can introduce the concept of abstract classes in the parent class. To hard limit the subclass must have certain method names.
Import ABC # Specifies the class attribute set metaclass is an abstract class, an abstract class itself is only used to constrain the subclass, can not be instantiated class Animal (metaclass = abc.ABCMeta): @ abc.abstractmethod # The subclass must limit decorator define a method called talk DEF talk (Self): # abstract methods without implementing specific functions Pass class Cat (Animal): # whenever inherited Animal subclass must follow standards set by the Animal DEF talk (Self): Pass CAT = Cat () # if not a subclass of method named talk thrown exception TypeError, can not be instantiated
But in fact, we can completely rely on inheritance, only need to create objects that have the same appearance and behavior, and we can also use objects regardless of the type of object. This is exactly the "duck typing" that Python advocates: "If it seems Like, screaming and walking like a duck, then it is a duck. " Compared with the inheritance method, the duck type achieves a degree of loose coupling of the program to some extent, as follows
# Both looks like a file, the file can be used when so as to use, but they are not directly related to class the Txt: # the Txt class has the same name as the file type with two methods, i.e., read and Write DEF read (Self) : pass def write (self): pass class Disk: # The Disk class also has two methods with the same name as the file type: read and write def read (self): pass def write (self): pass