Sometimes we write more than one class, so there can be inheritance between classes and classes.
For example:
#!/usr/bin/env python#-*-coding:utf-8-*-class father:def __init__ (self): Self.fname = ' FFF ' def func: print ' Father.func ' class Son (father): Def __init__ (self): self.sname = ' sss ' def bar (self): PRI NT ' son.bar ' S1 = Son () S1.bar () S1.func ()
Output Result:
Son.barfather.funcson. Smoking
As you can see from the example above
Methods in the parent class can be inherited and called by the Quilt class, called in the class. The method name in the parent class
Subclasses can inherit the parent class and override methods in the parent class
For example:
#!/usr/bin/env python#-*-coding:utf-8-*-class father:def __init__ (self): Self.fname = ' FFF ' def func: print ' Father.func ' def bad (self): print ' father. Smoking and drinking ' class son (father): Def __init__ (self): SE Lf.sname = ' sss ' def bar (self): print ' Son.bar ' def bad (self): print ' son. Smoking ' S1 = Son () s1.bar () S1.func ()
Output Result:
Son.barfather.funcson. Smoking
How does a subclass call the constructor of a parent class?
Executes the parent class name in the constructor of the subclass. __init__ (self)
#!/usr/bin/env python#-*-coding:utf-8-*-class father: def __init__ (self) : self.fname = ' FFF ' print ' Father.init ' def func (self): print ' Father.func ' def bad (self): print ' father, smoking and drinking ' Class son (father): def __init__ (self): self.sname = ' sss ' print ' Son.init ' father.__ Init__ (self) def bar (self): print ' Son.bar ' def bad (self): print ' son. Smoking ' s1&nBsp;= son () S1.bar ()
Output Result:
Son.initfather.initson.bar
Classic class and New class
The new class has an (object) identifier behind the class name.
The difference between a classic class and a new class:
In the case of multiple inheritance of a class, the classic class has a bug: it should be breadth first, but after multiple inheritance it becomes depth first.
#!/usr/bin/env python#-*-coding:utf-8-*-class a:def __init__ (self): print ' The ' A ' def Save (self): print ' Save from A ' class B (a): Def __init__ (self): print ' This is B ' class C (a): Def __init__ (self): p Rint ' This was C ' def Save (self): print ' Save from C ' class D (b,c): def __init__ (self): print ' This is D ' C = D () c.save ()
Output results
This was dsave from A
From the example above, Class D inherits the B and C two classes, and B precedence inheritance (when multiple inheritance, left precedence inheritance)
Since there is no Save method in class B and B inherits a, the Save method should be the Save method in Class A when inheriting B in D. D inherits the C class, so the last Sava method in the final class D should inherit Class C, but the result above is that D inherits the Class A. So this is an obvious bug.
We replaced the example with the new class to test the result:
#!/usr/bin/env Python#-*-coding:utf-8-*-class A (object): Def __init__ (self): print ' The ' A ' def save (self) : print ' Save from A ' class B (a): Def __init__ (self): print ' This is B ' class C (a): Def __init__ (self): print ' This was C ' def Save (self): print ' Save from C ' class D (b,c): def __init__ (self): print ' T He is d ' C = d () c.save ()
Output Result:
This was dsave from C
Summary: The new class fixes some bugs in the classic class and is fully compatible with the classic class. Therefore, it is recommended to use modern classes.
This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1858476
Inheritance such as Python