"Python" inheritance, multiple inheritance

Source: Internet
Author: User

1) Inheritance

Base class Baseclass:def __init__ (self,name,age): self.name = name Self.age = Age print "BaseClass I  S inited "Def speak (Self,name): print" base class is speak:%s "%name if __name__ = =" __main__ ": b = BaseClass ()
 derived class Import basemodelclass subclass (Basemodel.baseclass):     def __init__ (self,name,age,salary):         basemodel.baseclass.__init__ (self,name,age)    Call the function of the base class, need to add the class name          self.salary = salary        print  "Subclass  is inited and salary is:%s " %self.salary             def talk (SELF,STH):         print  "%s talking %s"   % (self,name,sth)          basemodel.baseclass.speak (SELF,STH)         if __name__  ==  "__main__":     s = subclass (' John ', 1,800)      S.talk ("A story") 

In the example above, the __init__ method is redefined in the derived class and, if not defined, by default calls the constructor inside the base class.


2) Multiple Inheritance

Multiple Inheritance Algorithm Class D (object): Def foo (self): print "Class D" class B (d): Pass class C (d): def foo (s ELF): Print "Class C" Class A (B, c): Pass F = A () F.foo () The result of the new class is Class C (a--b--c-d-object); the old class result is CLA SS D (A-b-d-c--object);


This article from "Kong Love to Eat fish" blog, declined reprint!

"Python" inheritance, multiple inheritance

Related Article

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.