What is the Python method rewrite? How do I override a parent class method?

Source: Internet
Author: User
In this article we will look at the method rewrite in Python, some friends may have just come into contact with Python, the programming language, for python rewrite This aspect of knowledge is relatively small, not clear about the method rewrite in Python knowledge. In the next article, let's take a look at a way to rewrite the method that Python overrides the parent class .

Method overrides

If the functionality of your parent's methods does not meet your needs, you can override the methods of your parent class in subclasses :


The example python overrides the parent class method is as follows

#!/usr/bin/python#-*-Coding:utf-8-*-class Parent:        # defines the parent class   def myMethod (self):      print ' calls the parent class method ' class child ( Parent): # definition Subclass   def myMethod (self):      print ' call subclass method ' C = Child ()          # Subclass Instance C.mymethod ()         # Subclass Call override method

Execute the above code to output the result as follows:

Calling Subclass Methods

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.