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 Class inheritance and Polymorphism in Python

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

Share

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

This article mainly introduces Python class inheritance and polymorphism example analysis, has a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian take you to understand.

concept

Class: Used to describe a collection of objects that have the same properties and methods.

Class variables: Class variables are common throughout the instantiated object. Class variables are defined in the class and outside the function body. Class variables are not usually used as instance variables.

The class has a special method (constructor method) called__init__(), which is called automatically when the class instantiates

self:self represents an instance of the class and represents the address of the current object, while self.class points to the class.

Class calls Car.weight

instantiate car01 =Car(5)

instance object calls car01.weght

Creating classes #Creating car and bus classes Car(object): def __init__(self,weight): #Passing in unique attributes when instantiating objects self.weight=weight print("car") print(weight,"t") #Defines an in-class method that prints the weight of an instantiated car def func(self): print("How to drive a car")class Passenger_car(object): def __init__(self,weight): self.weight=weight print("passenger car") print(weight,"t") def func(self): Inheritance of print("Driving method of passenger car") class

When we construct a class, Python 3 defaults to inheriting the base class object. I personally understand that object is an empty class. You can ignore why you want to write object in parentheses. This is a feature of Python 3. In Python2, if you do not write object, you will not inherit the base class object by default.

The same parent class we want to inherit from ourselves simply changes objetc to our own defined class name. Subclasses can have all the public properties and methods of their parent class, but they can be made private by underlining variable names so that they cannot access members of their parent class.

class Bus028(Passenger_car): def __init__(self,weight): #overrides of base class methods self.weight=weight print("Bus 28") print(weight,"t") def func(self): #Overrides of base class methods print("How to Drive Bus No. 28") #Subclasses inherit func methods from their parent classes, but modify the content of the original methods in their parent classes.

The parent class of the following three bus classes is the bus class. We can write a funcs method so that each time the funcs method is called, a different object is passed to execute a different func method. The specific implementation is as follows:

class Bus028(Passenger_car): def __init__(self,weight): self.weight=weight print("Bus 28") print(weight,"t") def func(self): print("How to Drive Bus No. 28")class Bus906(Passenger_car): def __init__(self,weight): self.weight=weight print("Bus 906") print(weight,"t") def func(self): print("How to Drive Bus 906")class BusB32(Passenger_car): def __init__(self,weight): self.weight=weight print("B32 Bus") print(weight,"t") def func(self): print("How to drive B32 bus")def funcs(obj): #where obj means passing in an instantiated object obj.func()

Main function:

from demo01 import *def main(): car01=Car(5) #instantiated as a concrete object with a weight of 5t car01.func() passenger_car01=Passenger_car(20) #instantiated as a concrete object with a weight of 20t passenger_car01.func() bus028_01=Bus028(15) #instantiated as a concrete object with a weight of 15t bus906_01=Bus906(15) #instantiated as a concrete object with a weight of 15t busB32_01=BusB32(15) #instantiated as a concrete object with a weight of 15t funcs(bus028_01) #Call funcs method funcs(bus906_01) funcs(busB32_01) if __name__=="__main__": main()

As you can see, I instantiate a car as a concrete object with a weight of 5t, a bus as a concrete object with a weight of 20t, and three buses as a concrete object with a weight of 15t.

As shown above, I pass in an instantiated object each time I call funcs methods, and funcs executes the corresponding internal methods depending on the object.

Note: (I am accustomed to reading methods as functions, and in future articles, methods are always replaced by functions)

Thank you for reading this article carefully. I hope that the article "Example Analysis of Class Inheritance and Polymorphism in Python" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

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