Detailed description of Python class inheritance instances and python inheritance instances

Source: Internet
Author: User

Detailed description of Python class inheritance instances and python inheritance instances

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!

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.