Object-Oriented Polymorphism in Python

       This article talks about polymorphism in object-oriented, and the premise and benefits of polymorphism from the refactoring of code. Remember this sentence first: The true meaning of polymorphism is: using a pointer or reference to a parent class, you can call an object of a subclass.

  • Observe the refactoring of the code first

class Animal:
    def talk(self):
        print('makeNoise')  # 动物都能发出声音

    def eat(self):  # 动物会吃
        print('eat')

    def sleep(self):  # 动物会睡觉
        print('sleep')


class Dog(Animal):
    def talk(self):
        print('Dog...barking')  # 狗汪汪

    def eat(self):
        print('Dog eating bone')

    def sleep(self):
        print('Dog sleeping')


class Pig(Animal):
    def talk(self):
        print('Pig...hem')  # 猪哼哼

    def eat(self):
        print('Pig eating qingCai')

    def sleep(self):
        print('Pig sleeping')


def use_dog(dog):
    dog.talk()
    dog.eat()
    dog.sleep()


# 我要开始养狗了
# 第一只
p1 = Dog()
p1.talk()
p1.eat()
p1.sleep()
# 第二只
p2 = Dog()
p2.talk()
p2.eat()
p2.sleep()
# 开始养猪
# 第一只
p1 = Pig()
p1.talk()
p1.eat()
p1.sleep()
# 第二只
p2 = Pig()
p2.talk()
p2.eat()
p2.sleep()

 Thinking: The more dogs are raised, the more objects. Method () calls

  • Look at the code in the red box, don't you think it's very similar? Only the object name is different, we are going to use methods (functions) to improve

class Animal:
    def talk(self):
        print('makeNoise')  # 动物都能发出声音

    def eat(self):  # 动物会吃
        print('eat')

    def sleep(self):  # 动物会发声
        print('sleep')


class Dog(Animal):
    def talk(self):
        print('Dog...barking')  # 狗汪汪

    def eat(self):
        print('Dog eating bone')

    def sleep(self):
        print('Dog sleeping')


class Pig(Animal):
    def talk(self):
        print('Pig...hem')  # 猪哼哼

    def eat(self):
        print('Pig eating qingCai')

    def sleep(self):
        print('Pig sleeping')


'''
以下为改进代码
'''


# 用函数改进
def use_dog(dog):
    dog.talk()
    dog.eat()
    dog.sleep()


# 同样的对cat一样
def use_pig(pig):
    pig.talk()
    pig.eat()
    pig.sleep()


# 我要开始养狗了
# 第一只
d1 = Dog()
use_dog(d1)
# 第二只
d2 = Dog()
use_dog(d2)
# 开始养猪
# 第一只
p1 = Pig()
use_pig(p1)
# 第二只
p2 = Pig()
use_pig(p2)

  • The call becomes concise

  •  Carefully observe the code in the red box in the figure below, only the object name is different.

  •  Refactor again

# 类的定义与上面的代码一样


'''
以下为改进代码
'''


def use_animal(animal):
    animal.talk()
    animal.eat()
    animal.sleep()


# 我要开始养狗了
# 第一只
d1 = Dog()
use_animal(d1)
# 第二只
d2 = Dog()
use_animal(d2)
# 开始养猪
# 第一只
p1 = Pig()
use_animal(p1)
# 第二只
p2 = Pig()
use_animal(p2)
  • Unify the functions of each animal with use_animal.

As can be seen from the above example, Dog and Pig are two subclasses of Animal. When calling the function, the parameter is specified as the Animal type, but the specific input is the object of the subclass of Anima Dog (d1, d2), Pig (p1, p2).

 When different (Dog, Pig) complete a certain behavior (talk, eat, sleep), different states will be produced (dogs have bones that dogs like to eat, cats have vegetables that cats like to eat), this is polymorphism.

Prerequisites for polymorphism:

  • Inheritance relationship For example: class Dog(Animal):
  • Method rewriting For example: def talk(self): print('Dog...barking') # dog barking
  • The parent class reference points to the subclass object. For example: use_animal(p1), the type of the parameter animal should be the Animal type when actually defined.

  • If I want to raise a fox (Fox) now, I just need to add the definition of the Fox class, and then call use_animal(fox object)
# 狐狸类定义
class Fox(Animal):
    def talk(self):
        print('Fox...whaaa')  # 狐狸哦哇

    def eat(self):
        print('Fox eating grape')

    def sleep(self):
        print('Fox sleeping')


'''
以下为改进代码
'''


def use_animal(animal):
    animal.talk()
    animal.eat()
    animal.sleep()



# 开始养狐狸
f1 = Fox()
use_animal(f1)
f2 = Fox()
use_animal(f2)

The overriding process from the above code has paid off the benefits of polymorphism:

  • Improved code maintainability
  • Improved code scalability

You can compare using polymorphism and not using polymorphism. Obviously, using polymorphic code is clearer and more concise. In addition, when we add a new subclass (Fox, Cock), it can be applied without changing the code. If polymorphism is not used, it is necessary to add use_fox, use_cock, use_xxx,... and other methods.

Guess you like

Origin blog.csdn.net/chinagaobo/article/details/127419574