One, polymorphic
Polymorphism refers to a class of things that have many forms
Animals have many forms: man, dog, pig
ImportABCclassAnimal (METACLASS=ABC. Abcmeta):#The same kind of things: animals@abc. AbstractmethoddefTalk (self):PassclassPeople (Animal):#one of the forms of animals: Man defTalk (self):Print('Say hello')classDog (Animal):#Animal Form Two: the Dog defTalk (self):Print('say Wangwang')classPig (Animal):#animal Form three: Pig defTalk (self):Print('say Aoao')
There are several forms of files: text files, executables
ImportABCclassFile (METACLASS=ABC. Abcmeta):#Same Kind of thing: file@abc. AbstractmethoddefClick (self):PassclassText (File):#one of the forms of the file: text file defClick (self):Print('Open File')classExefile (File):#form Two of the file: executable file defClick (self):Print('Execute File')
Ii. polymorphism
1. What is a polymorphic dynamic binding (sometimes polymorphic when used in an inherited context)
Polymorphism refers to the use of instances without regard to instance types
In the object-oriented approach, it is generally said that polymorphism: sending the same message to different objects (!!!) Obj.func (): Is the method func that called obj, also known as a message Func sent to obj, and different objects have different behavior (that is, methods) when they are received. In other words, each object can respond to a common message in its own way. The so-called message, is called the function, the different behavior refers to the different implementation, namely executes the different function. For example: Teacher. The bell rang (), the student. The bell rang (), the teacher performed the off-duty operation, the students performed the school operation, although the two messages, but the effect of the implementation of different
Detailed explanation
Polymorphism is divided into static polymorphism and dynamic polymorphism
Static polymorphism: If any type can be operated with operator +
Dynamic polymorphism: The following
peo=people () dog=Dog () pig=Pig ()#peo, dog, pig are all animals, as long as the animal must have talk method # So we don't have to think about what the specific type of the three are, but the direct use of Peo.talk () dog.talk () Pig.talk()# goes Further, We can define a unified interface to use def func (obj): obj.talk ()
2. Why use
The polymorphism and polymorphism of python--object-oriented