a polymorphic
Polymorphism refers to a class of things that have many forms
Eg: animals have many forms: cats, dogs, pigs
classAnimal:#Animal Class defEat (self):#Eat Pass defDrink (self):#Drink Pass defSleep (self):#Sleep PassclassCat (Animal):defJiao (self):Print(' Meow Meow')classDog (Animal):defCall (self):Print('Wang Woo')classPig (Animal):defHan (self):Print('Hum hum') C=Cat () d=Dog () p=Pig ()#Polymorphism : The method under which objects can be used without regard to the specific type of objectC.drink () D.drink () P.drink () C.sleep () D.sleep () P.sleep () C.jiao () D.call () P.han ()
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')
Two polymorphism
What is polymorphic dynamic binding (also known as polymorphism 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 and dynamic polymorphism
Static polymorphism: If any type can be operated with operator +
Dynamic polymorphism: The following
c=Cat () d=Dog () p=Pig ()#c,d,p are animals, as long as they are animals there must be a talk method # So we can not consider the specific type of the three of them, and directly using C.drink () d.drink () P.drink()# Further, we can define a unified interface to use def func (obj): obj.talk ()
two why to use polymorphism (the benefit of polymorphism)
In fact, we can see from the above polymorphism example, we have not added any new knowledge, that is, Python itself is to support polymorphism, what is the benefit of doing so?
1. Increased flexibility of the program
Status quo, regardless of the ever-changing object, the user is the same form to invoke, such as func (animal)
2. Increased scalability of the program
By inheriting the animal class, a new class is created, and the user does not have to change their own code or use Func (animal) to invoke the
Develop a set of procedures in the company, basically a few programmers work together to complete, so need to create a set of mandatory rules to follow the rule
Create a uniform set of rules with the base class, forcing subclasses to follow (using an abstract class implementation) so that you can use the method under the object two directly without the specific type of the object.
How to use polymorphic
ImportAbc#Import the ABC module to implement mandatory constraint subclasses
#子类必须有父类的方法, otherwise the instance session Typeerror:can ' t instantiate abstract class people with abstract methods eat
classAnimal (METACLASS=ABC. Abcmeta):#Animal Class@abc. AbstractmethoddefEat (self):#Eat Pass@abc. AbstractmethoddefDrink (self):#Drink Pass@abc. AbstractmethoddefSleep (self):#Sleep PassclassCat (Animal):defEat (self):#Eat Pass defDrink (self):#Drink Pass defSleep (self):#Sleep Print('Cat Sleeps')classpeople (Animal): #新添加 Another form of animal: Cat defEat (self):Pass defDrink (self):Pass defSleep (self):Print('people sleep') deffunc (animal): #对于使用者来说, your code doesn't have to change at all Animal.sleep () CAT1=Cat () peo1=people () #实例话出一个人func (peo1) #调用人的sleep功能func (CAT1)
three Duck type
Tease moment:
Python advocates duck type, that is, ' if it looks like, sounds like and walks like a duck, then it's a duck '
Python programmers usually write programs based on this behavior. For example, if you want to write a custom version of an existing object, you can inherit the object
You can also create a new object that looks and behaves like, but has nothing to do with it, which is typically used to preserve the loose coupling of program components.
Example 1: Use the various ' file-like ' objects defined in the standard library, although they work like files, but they do not inherit the method of the built-in file object
# Both are like ducks, and both look like files, so they can be used as files . class txtfile: def Read (self): Pass def Write (self): Pass class diskfile: def Read (self): Pass def Write (self): Pass
Example 2: In fact, everyone has been enjoying the benefits of polymorphism, such as Python's sequence type has a variety of forms: string, list, tuple, polymorphism embodied as follows
# str,list,tuple are sequence types s=str ('hello') L=list ([]) T=tuple ((4,5,6)) # we can use S,l,tS.__len__() L without regard to the three types. __len__ () T. __len__ () Len (s) Len (l) Len (t)
Python Tour: Object-oriented polymorphism, polymorphism