Cainiao says inheritance in python

Source: Internet
Author: User

Python supports the object-oriented programming style. Here we mainly talk about multiple inheritance in python:

The following code uses python2.4 and the idle IDE development environment after installation (IDE is much simpler than delphi and VS.net)

Create a. py File from the File-New menu and write down the following code:

Class SuperClass:

Def sample (self ):

Print 'superclass'

Class SubClass (SuperClass ):

Pass

Sub = SubClass ()

Sub. sample ()

Save the file and press F5 to display it in the main idle window:

>>>================================== RESTART ==== ======================================

>>>

SuperClass

>>>

The subclass calls the sample method of the parent class. The code is modified as follows:

Class SuperClass:

Def sample (self ):

Print 'superclass'

Class SuperClass1:

Def sample (self ):

Print 'superclass1'

Class SubClass (SuperClass, SuperClass1 ):

Pass

Sub = SubClass ()

Sub. sample ()

Run the code and the result is the same as the above. Here, the subclass calls the sample method of the first parent class, and the second parent class is not called,

Now you know what to say. Change the declaration of the SubClass class:

Class SubClass (SuperClass1, SuperClass ):

Pass

Run. The result is that the SuperClass1 sample method is called.

>>>================================== RESTART ==== ======================================

>>>

SuperClass1

>>>

Here, we can see that when multiple inheritance occurs, the same method in the parent class calls the method of the first parent class declared by the class in the subclass.

Related Article

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.