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!