Class inheritance-multiple inheritance (two parent classes have the same method name and parameter)

Source: Internet
Author: User
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.