Python inheritance and object-oriented parsing

Source: Internet
Author: User
The Python programming language is a powerful development language, and its greatest feature is its ease of use. But also has the object-oriented characteristic, can help us to realize some specific function requirement very well. We will be here today to give you a detailed description of the concepts related to Python inheritance.

The following code uses Python2.4, using the Idle IDE development environment after installation (say IDE, much simpler than delphi,vs.net)
To create a. py file from the File-new menu, write 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 ()

To save first, then press F5 to execute, in the Idle main window display:
The subclass calls the sample method of the parent class and now modifies the code 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 python to inherit the code, see the result is the same as above, where the subclass called the first parent class of the sample method, the second parent class is not called, now know what to say, and then change the subclass class declaration to:

Class Subclass (Superclass1,superclass): 1.pass

Run, see the result is SuperClass1 the sample method is called.

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

Here, it can be seen that in the case of Python inheritance, the same method in the parent class invokes the method of the first parent class declared by the class in the child class.

The above is the Python inheritance and object-oriented parsing content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.