current position:Home>Object-Oriented Polymorphism in Python

Object-Oriented Polymorphism in Python

2022-11-08 05:08:13Hello 0 1

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

  • Watch the code refactoring first

class Animal:def talk(self):print('makeNoise') # Animals can make noisedef eat(self): # animals will eatprint('eat')def sleep(self): # Animals sleepprint('sleep')class Dog(Animal):def talk(self):print('Dog...barking') # dog barkingdef eat(self):print('Dog eating bone')def sleep(self):print('Dog sleeping')class Pig(Animal):def talk(self):print('Pig...hem') # Pig hummingdef eat(self):print('Pig eating qingCai')def sleep(self):print('Pig sleeping')def use_dog(dog):dog.talk()dog.eat()dog.sleep()# I'm going to start raising a dog# firstp1 = Dog()p1.talk()p1.eat()p1.sleep()# secondp2 = Dog()p2.talk()p2.eat()p2.sleep()# start raising pigs# firstp1 = Pig()p1.talk()p1.eat()p1.sleep()# secondp2 = Pig()p2.talk()p2.eat()p2.sleep()

Thinking: The more dogs you have, the more object.method() calls will be made

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

class Animal:def talk(self):print('makeNoise') # Animals can make noisedef eat(self): # animals will eatprint('eat')def sleep(self): # Animals make soundsprint('sleep')class Dog(Animal):def talk(self):print('Dog...barking') # dog barkingdef eat(self):print('Dog eating bone')def sleep(self):print('Dog sleeping')class Pig(Animal):def talk(self):print('Pig...hem') # Pig hummingdef eat(self):print('Pig eating qingCai')def sleep(self):print('Pig sleeping')'''The following is the improved code'''# Improve with functionsdef use_dog(dog):dog.talk()dog.eat()dog.sleep()# same for catdef use_pig(pig):pig.talk()pig.eat()pig.sleep()# I'm going to start raising a dog# firstd1 = Dog()use_dog(d1)# secondd2 = Dog()use_dog(d2)# start raising pigs# firstp1 = Pig()use_pig(p1)# secondp2 = Pig()use_pig(p2)

  • Calls become concise

  • Carefully observe the code in the red box in the figure below, only the object names are different.

  • Refactor again

# The class definition is the same as the code above'''The following is the improved code'''def use_animal(animal):animal.talk()animal.eat()animal.sleep()# I'm going to start raising a dog# firstd1 = Dog()use_animal(d1)# secondd2 = Dog()use_animal(d2)# start raising pigs# firstp1 = Pig()use_animal(p1)# secondp2 = Pig()use_animal(p2)
  • Unify the functions of each animal with use_animal.

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

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

Prerequisites for polymorphism:

  • Inheritance relationship such as: class Dog(Animal):
  • Method overriding For example: def talk(self): print('Dog...barking') # Dog barking
  • The parent class reference points to the child class object For example: use_animal(p1), in fact, the type of parameter animal should be Animal type when it is defined.

  • If I now want to raise a fox (Fox), I just need to add the definition of the Fox class and call use_animal(fox object)
# Fox class definitionclass Fox(Animal):def talk(self):print('Fox...whaaa') # Fox oh wahdef eat(self):print('Fox eating grape')def sleep(self):print('Fox sleeping')'''The following is the improved code'''def use_animal(animal):animal.talk()animal.eat()animal.sleep()# start raising foxesf1 = Fox()use_animal(f1)f2 = Fox()use_animal(f2)

The benefits of polymorphism have been repaid from the repetition of the code above:

  • Improved code maintainability
  • Improved code scalability

You can compare the use of polymorphism with and without polymorphism, it is clear that using polymorphism code is cleaner and more concise.In addition, when we add new subclasses (Fox, Cock), it can be applied without changing the code. If polymorphism is not used, we need to add use_fox, use_cock, use_xxx, ... and other methods.

copyright notice
author[Hello 0 1],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/312/202211080453104660.html

Random recommended