1. The polymorphism
""" Where you need to use a parent class object, you can also pass in a subclass object to get different results ---- Polymorphic implementation steps: 1. Subclasses inherit from the parent class. 2. Subclasses override methods of the same name in the parent class. 3. Call a method in a method whose subclass has the same name as its parent class.
# 1. Define DOg class
class Dog(object) :
def __init__(self, name) :
self.name = name
def play(self) :
print(F the dog '{self.name}In the play... ')
# 2. Define crouching dogs and inherit the Dog category
class XTQ(Dog) :
# 3. Override the Play method
def play(self) :
print(f'{self.name}Chasing clouds in the sky..... ')
# 4. Define a common approach,
def play_with_dog(obj_dog) :
obj_dog.play()
# create Dog object @
dog = Dog('rhubarb')
play_with_dog(dog)
Create an object of class XTQ
xtq = XTQ('black')
play_with_dog(xtq)
Copy the code