Detailed description of the Python class inherited instance code

Source: Internet
Author: User
This article mainly introduces detailed information about the inheritance instances of the Python class. For more information about the inheritance instances of the Python class, see the following article, for more information, see

Python class inheritance

Since Python is object-oriented, it certainly supports class inheritance. the inheritance of Python implementation classes is simpler than that of Javascript.

Parent class:

class Parent:    parentAttr = 100    def init(self):     print("parent Init")    def parentMethod(self):     print("parentMethod")      def setAttr(self,attr):     self.parentAttr = attr    def getAttr(self):     print("ParentAttr:",Parent.parentAttr)

Child class

class Child(Parent):    def init(self):     print("child init")    def childMethod(self):     print("childMethod")

Call

p1 = Parent(); p1.parentMethod();  c1 = Child(); c1.childMethod();

Output:

parent Init parentMethod child init childMethod Press any key to continue . . .

Python supports multi-inheritance

Class A: # define class ..... class B: # define class B ..... class C (A, B): # inherit classes A and B .....

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

The above is a detailed description of the Python class inherited instance code. For more information, see other related articles in the first PHP community!

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.