Home  >  Article  >  Backend Development  >  What is python method overriding? How to override parent class method?

What is python method overriding? How to override parent class method?

乌拉乌拉~
乌拉乌拉~Original
2018-08-20 14:40:075187browse

In this article, let’s learn about method rewriting in python. Some friends may have just come into contact with the programming language python and have no knowledge of python rewriting I don’t know much about method rewriting in python. In the next article, we will learn about the method rewriting that can be done python rewrites the parent class method.

Method overriding

If the function of your parent class method cannot meet your needs, you can override it in subclassyour parent class Class method:


The example of python overriding the parent class method is as follows

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class Parent:        # 定义父类
   def myMethod(self):
      print '调用父类方法'
 
class Child(Parent): # 定义子类
   def myMethod(self):
      print '调用子类方法'
 
c = Child()          # 子类实例
c.myMethod()         # 子类调用重写方法

The output result of executing the above code is as follows:

调用子类方法

Above That’s all the content described in this article. This article mainly introduces the relevant knowledge of python object rewriting. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What is python method overriding? How to override parent class method?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more