Nothing to say, inheritance is mainly inherited by some parents, the code is very specific
#!/usr/bin/env python #coding: Utf-8class Father (object): #新式类 def __init__ (self): self.name= ' Liu ' self . Familyname= ' Yan ' def Lee (self): print ' I am the parent function Lee ' def Allen (self): print "I am the parent function Allen" class Son (Father): def __init__ (self): #Father. __init__ (self) #经典类运行父类构造函数 Super (son,self). __init__ () #新式类运行父类构造函数 self.name= ' Feng ' def Aswill (self): #子类新增函数 print ' Son.bar ' def Lee: #重写父类函数Lee print ' subclass overrides the parent class function Lee ' s1=son () print "inherits the parent's last name" + S1. Familynameprint "Overrides the name of the parent class", S1.names1.Lee () #子类重写了父类函数Lees1. Allen () #子类继承了父类函数Allen
The order of inheriting multiple classes, the classic class inheritance is depth first, is a bug, the new class is breadth first, should be to use the new class to define the class
New class
Class A (object): #新式类的写法 def __init__ (self): print ' This is from A ' def Test (self): print ' The IS test fr Om A ' class B (a): def __init__ (self): print "The IS from B" class C (a): def __init__ (self): print " This was from C " def Test (self): print" The IS test from C " class D (b,c): def __init__ (self): pri NT ' This is D ' t1=d () t1.test ()
Classic class
Class A: def __init__ (self): print ' The From a ' def Test (self): print ' This is test from a ' class B (a): def __init__ (self): print "The IS from B" class C (A): def __init__ (self): print "The" from C "
def Test (self): print "The IS test from C" class D (b,c): def __init__ (self): print ' This is D '
t1=d () t1.test ()
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Python Object-oriented inheritance