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