Python basic programming -- method call under multiple inheritance

We introduced how to call the construction method of the parent class under inheritance. How to call the method of the parent class under multiple inheritance? Although we need to be careful when using multiple inheritance, multiple inheritance can be seen everywhere in actual project development. Therefore, it is necessary to understand the methods of calling parent classes under multiple inheritance.

Construction method of calling parent class under multiple inheritance

Under multiple inheritance, if the constructor is explicitly overridden in the parent class, the method of the parent class needs to be called in the child class. When the parameter needs to be passed in the parent class, we also need to pass in the corresponding parameter in the child class. However, in order to avoid multiple inheritance errors, we need to use variable length parameters to receive parameters (this is only one of the methods).

The details are as follows: many_extends_call_construction_method.py

The procedure is as follows:

many_extends_call_construction_method.py
#!/usr/bin/env python# -*- coding: utf-8 -*-
# Define a Person class class Person:    
# Define construction method    
def __init__(self, name, age, *args, **kwargs):        
self.name = name        
self.age = age        
print('call Person Class construction method')
# Define a Student class class (person):    
# Define construction method    
def __init__(self, name, age, student_no, native_place, *args, **kwargs):        
super()
.__init__(name, age, *args, **kwargs)        
self.student_no = student_no        
self.native_place = native_place        
print('call Student Class construction method')
# Define a Teacher class
class Teacher(Person):    
def __init__(self, name, age, course, *args, **kwargs): 
super().__init__(name, age, *args, **kwargs)

self.course = course        
print('call Teacher Class construction method')
# Define a multi professional class, inherited from the class of students and teachers
class MultipleProfessional(Student, Teacher):    
def __init__(self, name, age, student_no, native_place, course, hobby): 

super().__init__(name, age, student_no, native_place, course)        
self.hobby = hobby        
print('call MultipleProfessional Construction method of')
print(MultipleProfessional.__mro__)
mp = MultipleProfessional('Zhang San', 20, '20201001', 'Beijing', 'Python', 'programming')
The program execution results are as follows:
(<class '__main__.MultipleProfessional'>, 
<class '__main__.Student'>, <class '__main__.Teacher'>, 
<class '__main__.Person'>, <class 'object'>)
call Person Class Teacher Class
Student Class MultipleProfessional Construction method of

In the above program, we use variable length parameters in each class to receive parameters to avoid error reporting under multiple inheritance. Under multiple inheritance, through__ mro__ You can see the order of multiple inheritance and the order of calling each parent class constructor. Finally, if your time is not very tight and you want to improve quickly, the most important thing is not afraid of hardship. I suggest you can contact Wei: 762459510. That's really good. Many people make rapid progress and need you to be not afraid of hardship! You can add it and have a look~

Common method of calling parent class under multiple inheritance

Since behoove is inherited, subclasses should have all the attributes and methods of the parent class. One of the main functions of inheritance is to make the programs we write can be reused, so it is natural to call the generic methods of the parent class in subclasses. Of course, subclasses can also have their own properties and methods. When the properties and methods provided by the parent class cannot meet the requirements, we can customize other properties and methods, or override the methods of the parent class to achieve the purpose. The details are as follows: many_extends_call_method.py program is shown as follows:

many_extends_call_method.py
#!/usr/bin/env python# -*-coding: utf-8 -*-
# Define a Person class class Person:    
# Define construction method    
def __init__(self, name, age, *args, **kwargs):        
self.name = name        
self.age = age        
print('call Person Class construction method')    
def speak(self):        
print('Hello! My name is{},Age is{}. 
'.format(self.name, self.age))
# Define a Student class class (person):    
# Define construction method    
def __init__(self, name, age, student_no, native_place, *args, **kwargs):  
super().__init__(name, age, *args, **kwargs)        
self.student_no = student_no        
self.native_place = native_place        
print('call Student Class construction method')    
def student_say_hi(self):        
print('Hello! I am a student. My name is:{},
My age is:{},My student number is:{},I come from:{}. 
'.format(self.name, self.age, self.student_no,                                                                     
self.native_place))
# Define a Teacher class class Teacher(Person):    
def __init__(self, name, age, course, *args, **kwargs):
super().__init__(name, age, *args, **kwargs)        
self.course = course        
print('call Teacher Class construction method')    
def teacher_say_hi(self):        
print('Hello! I am a teacher. My name is:{},
My age is:{},The courses I teach now are:{}. 
'.format(self.name, 
self.age, self.course))
# Define a multi professional class, inherited from the class of students and teachers
class MultipleProfessional(Student, Teacher):    
def __init__(self, name, age, student_no, native_place, course, hobby): 
super().__init__(name, age, student_no, native_place, course)        
self.hobby = hobby        
print('call MultipleProfessional Construction method of')    
def mp_say_hi(self):        
print('Hello! I am a student and a teacher. My name is:{},
My age is:{},My student number is:{},I come from:{},

The courses I teach are:{},My hobby is'              ': {}. 
'.format(self.name, self.age, self.student_no, 
self.native_place, self.course, self.hobby))
print(MultipleProfessional.__mro__)
mp = MultipleProfessional('Zhang San', 20, '20201001',
'Beijing', 'Python', 'programming')
mp.speak()mp.student_say_hi()
mp.teacher_say_hi()mp.mp_say_hi()
The program execution results are as follows:
(<class '__main__.MultipleProfessional'>, 
<class '__main__.Student'>,
<class '__main__.Teacher'>, 
<class '__main__.Person'>, 
<class 'object'>)
call Person Class Teacher Class
Student Class MultipleProfessional Construction method of
 Hello! My name is Zhang San and my age is 20.
Hello! I am a student. My name is:
Zhang San, my age is 20. My student number is 20201001. I come from Beijing.

Hello! I am a teacher. My name is Zhang San. My age is 20. The courses I teach now are: Python. 

Hello! I am a student and a teacher. My name is Zhang San. My age is 20. My student number is 20201001,
I'm from Beijing. My courses are: Python,My hobby is: programming.

In the above program, we call some methods that are not defined in the subclass object, but the program does not report errors, because these methods are inherited from the parent class. When the method in the parent class does not meet the needs, we redefine a method that belongs to the subclass only. If we want to call the method or property of the parent class in the definition of the subclass, we can use the self keyword or the super function, but it should be noted that the call needs to be behind the construction method of the subclass, or if there is no such attribute or method error in the newspaper. Finally, if your time is not very tight and you want to improve quickly, the most important thing is not afraid of hardship. I suggest you can contact Wei: 762459510. That's really good. Many people make rapid progress and need you to be not afraid of hardship! You can add it and have a look~

summary

This section introduces the properties and methods (including construction methods) of calling parent classes under multiple inheritance, but multiple inheritance should be avoided in actual development. When multiple inheritance is not necessary to solve problems, some design patterns should be considered to replace multiple inheritance instead of using multiple inheritance directly.

If there is a need for small and medium-sized programs, you can send me a private letter!

It's not easy to create. Please praise it! Please pay attention and collect your favorite friends!

Welcome to forward and comment!

Keywords: Python Java Programming AI crawler

Added by Gnub on Mon, 27 Dec 2021 23:30:40 +0200