Python inheritance and object-oriented analysis

Source: Internet
Author: User
The Python programming language is a powerful development language. its biggest feature is its ease of use. It also has object-oriented features, which can help us implement some specific functional requirements. We will introduce the concept of Python inheritance in detail here today. The Python programming language is a powerful development language. its biggest feature is its ease of use. It also has object-oriented features, which can help us implement some specific functional requirements. We will introduce the concept of Python inheritance in detail here today.

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 Python inheritance code:

>>>=============== RESTART ======================
>>> Class SuperClass: 1.def sample (self ):
Print 'superclass'
2. class SubClass (SuperClass ):
Pass
3. sub = SubClass ()
4. sub. sample ()

Save the file and press F5 to display it in the main idle window:
The subclass calls the sample method of the parent class. the code is modified as follows:

Class SuperClass: 1.def sample (self ):
Print 'superclass'
2. class SuperClass1:
Def sample (self ):
3. print 'superclass1'
Class SubClass (SuperClass, SuperClass1 ):
4. pass
Sub = SubClass ()
5. sub. sample ()

Run the Python inheritance code. 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): 1. pass

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

>>>=============== RESTART ====================== 1. >>>
SuperClass1
2. >>>

Here, we can see that when Python inherits, the same method in the parent class will call the method of the first parent class declared by the class in the subclass.

The above is the Python inheritance and object-oriented parsing content. For more information, please follow the PHP Chinese network (www.php1.cn )!

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.