Python implements multiple inheritance examples with and without parameters.
This example describes how to implement multi-inheritance with and without parameters in Python. We will share this with you for your reference. The details are as follows:
1. Multi-inheritance without Parameters
# Author: hhh5460 # Time: 2017.07.18class A (object): def show_x (self): print ('A') class B (object): def show_y (self ): print ('B') class C (object): def show_z (self): print ('C') class D (A, B, C ): pass # test if _ name _ = '_ main _': d = D () d. show_x () # A d. show_y () # B d. show_z () # C
2. Multi-inheritance with Parameters
# Author: hhh5460 # Time: 2017.07.18class A (object): def _ init _ (self, x = 0): self. _ x = x def show_x (self): print (self. _ x) def show_name (self): print ('A') class B (object): def _ init _ (self, y = 0): self. _ y = y def show_y (self): print (self. _ y) def show_name (self): print ('B') class C (object): def _ init _ (self, z = 0): self. _ z = z def show_z (self): print (self. _ z) def show_name (self): print ('C') # note that the following two types of D and E are inherited from A, B, C, and Class A has the highest priority. However, the order of the three _ init _ statements is the opposite class D (A, B, C): def _ init _ (self, x = 0, y = 0, z = 0): C. _ init _ (self, z) # init c B. _ init _ (self, y) # init B. _ init _ (self, x) # init A (A top priority) class E (A, B, C): def _ init _ (self, x = 0, y = 0, z = 0): super (E, self ). _ init _ (x) # init A (A top priority) # This sentence can be abbreviated as: super (). _ init _ (x) super (A, self ). _ init _ (y) # init B super (B, self ). _ init _ (z) # init C # test if _ name _ = '_ main _': d = D (1, 2, 3) d. show_x () #1 d. show_y () #2 d. show_z () #3 d. show_name () # A e = E (1, 2, 3) e. show_x () #1 e. show_y () #2 e. show_z () #3 e. show_name () #