Deep understanding of Python polymorphism in Python tutorial
- 2021-11-30 00:35:10
- OfStack
Foreword:
The realization of polymorphism must meet two preconditions
1. Inheritance: Polymorphism 1 must occur between a child class and a parent class 2. Override: A polymorphic subclass overrides the parent's methodRemember these two points and combine them with code examples to help understand polymorphism
# Polymorphic code examples
class Farther:
def behavior(self,action):
print(" Fathers' Association %s" % action)
# Definition Son Class inheritance Farther Class
class Son(Farther):
def behavior(self,action):
print(" Sons will %s" % action)
# Definition Grandson Class inheritance Farther Class
class Grandson(Farther):
def behavior(self,action):
print(" Sun Tzu Club %s" % action)
# Use Farter Class to create an object
# The program is determined when it is executed behavior Where does the method belong 1 Object of
f = Farther()
print(id(f))
f.behavior(" Eat ") # Father can eat
f = Son()
print(id(f))
f.behavior(" Eat ") # My son can eat
f = Grandson()
print(id(f))
f.behavior(" Eat ") # Grandson can eat
Run results:
1603040478928
Father can eat
1603040477584
Grandson can eat
1603040478928
My son can eat
As can be seen from the above code example, both Son and Grandson subclasses inherit the Farther class and both override the behavior method in the parent class; It can be seen from this result that when the same object calls the same behavior method (with identical parameters and function names), the program does not call the same behavior () method in actual operation because the instance object pointed by f is different, and the code will automatically decide which behavior method to execute according to the specific object of p, which is called polymorphism
# Application of polymorphism
class IceCream:
def make_icecream(self,taste):
print(" Looking for different flavors of ice cream ...")
taste.make(self)
# Defining matcha-flavored ice cream and method
class Tea:
def make(self,make_icecream):
print('%s The production method of matcha is being called ' % make_icecream)
class Orange:
def make(self,make_icecream):
print('%s The production method of matcha is being called ' % make_icecream)
class Apple:
def make(self,make_icecream):
print('%s The production method of matcha is being called ' % make_icecream)
i = IceCream()
# Matcha flavor
i.make_icecream(Tea())
# Orange flavor
i.make_icecream(Orange())
# Apple flavor
i.make_icecream(Apple())
# Implementation results
Looking for different flavors of ice cream...
< __main__.IceCream object at 0x000002700CF086D0 > The production method of matcha is being called
Looking for different flavors of ice cream...
< __main__.IceCream object at 0x000002700CF086D0 > The production method of matcha is being called
Looking for different flavors of ice cream...
< __main__.IceCream object at 0x000002700CF086D0 > The production method of matcha is being called
Analysis:
It can be seen from the above code that when calling make_icecream () method of IceCream class, the program does not care who the parameter object taste passed in for this method is, only requires that this parameter object taste contains make () method, and Python does not care whether the parameter object type taste passed in by the caller is a subclass or another class!
To sum up, different subclass objects in polymorphism call the same parent class method, which will produce different execution results
Polymorphism here means that functions with different functions can use the same function name, so that functions with different contents can be called with one function name.
class Wechatpay:
def __init__(self, name, money):
self.name = name
self.money = money
def pay(self):
print('%s Paid through WeChat %s Yuan ' % (self.name, self.money))
class Alipay:
def __init__(self, name, money):
self.name = name
self.money = money
def pay(self):
print('%s Paid through Alipay %s Yuan ' % (self.name, self.money))
# The polymorphism here is reflected in the same direction 1 After passing different parameters, different functions can be realized .
def pay(person):
person.pay()
# Realized object
ali = Alipay("xiaoming", 100)
wch = Alipay("xiaohong", 100)
pay(ali)
pay(wch)
The above is the Python tutorial Python polymorphism in-depth understanding of the details, more information about Python polymorphism understanding please pay attention to other related articles on this site!