1. Simple inheritance
class F1: # parent class, or base class def Show (self): print(' F1.show')class F2 (F1): # indicates F2 inherits F1. F2 is called a subclass, or a derived class def bar (self ):print('bar ' = F2 () obj.show () Obj.bar ()
Out
F1.show
Bar
2. The parent class and subclass have the same method, and the methods in the subclass are taken precedence
classF1:#parent class, or base class defShow (self):Print('F1.show')classF2 (F1):#represents the F2 inheritance F1. F2 called subclasses, or derived classes defBar (self):Print('Bar') defShow (self):#sub-class priority high Print('F2.show') obj=F2 () obj.show () Obj.bar ( )
Out
F2.show
Bar
3. Encapsulates data in subclasses, and can also be accessed in the parent class
classF1:#parent class, or base class defShow (self):Print('F1.show') deffoo (self):Print(Self.name)classF2 (F1):#represents the F2 inheritance F1. F2 called subclasses, or derived classes def __init__(self,name): Self.name=namedefBar (self):Print('Bar') obj= F2 ('xiaoming') Obj.foo ()#The output is xiaoming, and inheritance is equivalent to putting the parent class's method into the subclass, so the output here is Xiaoming
Out
4. Complex inheritance
classS1:defF1 (self): self. F2 ()#the self at this point corresponds to the object obj defF2 (self):Print('S1.f2')classS2 (S1):defF3 (self): self. F1 ()defF2 (self):Print('S2.f2') obj=S2 () obj. F3 ()#The output is s2.f2, the execution process: executes the self when executing the F3 method in the S2 class. F1 () method, because there is no method in S2, so go to the parent class to look for, the parent class has the method,#then the F1 () method in the parent class is executed, and self is executed when the F1 () method is executed. F2 (). Because the essence of inheritance is to copy the method of the parent class into the subclass,#So Def F1 (self): self. F2 () is placed in the S2 class because there is a F2 method in the S2 class, so the S2 () method in the F2 class is executed. Subclass methods have high precedence when subclasses and parent classes have the same method. #just go to self. F2 () in such a time, go straight back to the original point to look for. For example, the example should go back to the S2 class for the F2 () method,#the method is found in subclass S2 and is executed directly. If not in the S2 class, look for the F2 () method in the parent class
Second, multiple inheritance
1. Simple Multiple inheritance
classC0:defF1 (self):Print('c0_f1')classC1 (C0):defF2 (self):Print('C1_f2')classC2:defF2 (self):Print('C2_f2')classC3 (C2,C1):#inherit from left to right, high priority on left defF3 (self):Passobj=C3 () obj.f2 ( )#Output C2_F2
Out
C2_f2
2. Complex multiple inheritance
classC0:defF2 (self):Print('c0_f1')classC1 (C0):defF1 (self):Print('C1_f2')classC2:defF2 (self):Print('C2_f2')classC3 (C1,C2):#inherit from left to right, high priority on left defF3 (self):Passobj=C3 () obj.f2 ( )#Output C0_F1
Out
C0_f1
Execution process:
3. Complex inheritance Relationship
classc_2:defF2 (self):Print('C_2_f2')classc_1 (c_2):defF2 (self):Print('C_1_f2')classC0 (c_2):defF1 (self):Print('C0_f2')classC1 (C0):defF1 (self):Print('C1_f2')classC2 (c_1):defF2 (self):Print('C2_f2')classC3 (C1,C2):#inherit from left to right, high priority on left defF3 (self):Passobj=C3 () obj.f2 ( )#Output C0_F1
Out
C2_f2
Execution process
Summarize:
If the subclass's two parent class does not have a common parent class, it is a way to go to the black
If the subclass's two parent class has a common parent class, it is c1-->c0 and then c2-->c_1-->c_2
Inheritance of the python-class