Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Polymorphism in python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of polymorphism in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Polymorphisms

Polymorphism: as the name implies, there are many forms in 1994, that is, the same behavior has different behaviors to different subclasses [objects].

In order to achieve polymorphism, there must be two prerequisites:

1. There must be an inheritance relationship that must occur between the parent and subclasses

two。 Override: the subclass overrides the method of the parent class

The advantages of polymorphism:

Polymorphism can increase the flexibility of programs.

Increase the expansibility of the program

As long as there is a method, no matter who the inheritor is or who the definer is, you can call it.

Case demonstration class Animal: def say_who (self):''parent class [base class]: return:' 'print (' you are an animal') pass passclass Duck (Animal):''inherits from the duck subclass [subclass] derived class' 'def say_who (self): The method of rewriting the parent class here: return:''print (' I am a duck') passclass Dog (Animal): def say_who (self): print ('just a woof') pass pass duck1=Duck () duck1.say_who () dog=Dog () dog.say_who ()

Class Animal: def say_who (self):''parent class [base class]: return:' 'print (' you are an animal') pass passclass Duck (Animal):''inherits from the duck class animal [subclass] derived class' 'def say_who (self): Rewrite the method of the parent class here: return:''print (' I'm a duck') passclass Dog (Animal): def say_who (self): print ('just a woof') pass pass # uses a unified function to call def commonInvoke (obj):''agreed to the calling method : param obj: instance of the object: return:''obj.say_who () # duck1=Duck () # duck1.say_who () # dog=Dog () # dog.say_who () listObj= [Duck () Dog ()] for item in listObj:''loop call function' 'commonInvoke (item) # is called uniformly through the function

There is no need to modify the code to add new classes. Just add the required code # case demonstration class Animal: def say_who (self):''parent class [base class]:' 'print (' you are an animal') pass passclass Duck (Animal):''inherits from the duck class of animal [subclass] Class''def say_who (self):' 'the method of rewriting the parent class here: return:' 'print (' I am a duck') passclass Dog (Animal): def say_who (self): print ('just a Wang') pass passclass Bird (Animal): def say_who (self) Print (under the bridge in front of the door Stop a bird') # use a unified function to call def commonInvoke (obj):''agreed to call the method: param obj: object instance: return:' 'obj.say_who () # duck1=Duck () # duck1.say_who () # dog=Dog () # dog.say_who () listObj= [Duck (), Dog () Bird ()] for item in listObj:''loop call function' 'commonInvoke (item) # is called uniformly through the function

This is the end of this article on "sample analysis of polymorphism in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report