1 #-*-coding: gb2312-*-2 3 # function: Test multi-inheritance 4 5 Class A: 6 7 def F1 (Self): 8 print 'Call. f1 () '9 10 def F2 (Self): 11 print 'Call. f2 () '12 self. f1 () 13 14 def F3 (Self): 15 print 'Call. f3 () '16 self. f1 () 17 18 19 Class B: 20 21 def F2 (Self): 22 print 'Call B. f2 () '23 24 def F3 (Self): 25 print' call B. f3 () '26 27 class C (A, B): 28 29 def F4 (Self): 30 print 'C. f4 '31 self. f2 () 32 self. f3 () 33 34 C = C () 35 C. f4 ()
The meaning of this program:
Define Class A (including Methods F1, F2, F3, F2, and F3 call F1 respectively) and define class B (including Methods F2 and F3)
Define class C (inherited from Class A and Class B), define method F4, call the inherited methods F2 and F3
The running result is as follows:
C. F4
Call a. F2 ()
Call a. F1 ()
Call a. F3 ()
Call a. F1 ()
Step 2:
Replace the inheritance sequence of the definition Class C, that is, the definition is as follows, and the others remain unchanged:
Class C (B, ):
Then run the program. The running result is as follows:
C. F4
Call B. F2 ()
Call B. F3 ()
It indicates that the interpreted language like python will not be pre-compiled, and the corresponding function of a will be first searched during runtime. If it is found, the corresponding function of a will be called directly; otherwise, the corresponding function of B will be searched.
However, an error occurs in the compilation phase of an interpreted language (such as C ++) program.