A succession
Class inheritance with real-life father and son, grandson, great-grandson and so on, as the parent class is also known as the base class
In python inheritance is divided into single and multiple inheritance
Father class: Pass class Mother: Pass class Son (Father): # single inheritance Pass class Son (Father, Mother): # multiple inheritance (can be more than two) Pass
Subclass integrates all the class attributes of the parent class
class Father: money = 100 def __init__(self,name, age): self.name = name self.age = age def play_son(self): print("%s正在打儿子"%self.name) class Son(Father): pass f1 = Father("蔡徐坤","30") print(Son.money) Son.play_son(f1)
If the child class defines the attributes with the same name as the parent class, and their definition of value take priority
class Father: money = 100 def __init__(self,name, age): self.name = name self.age = age def play_son(self): print("%s正在打儿子"%self.name) class Son(Father): money = 10 def play_son(): print("测试") f1 = Father("蔡徐坤","30") print(Son.money) Son.play_son()
Because the Son is not following example init function, it is instantiated when the trigger Son inherits the parent class init, so the need to pass two parameters
Father class: Money = 100 DEF __init __ (Self, name, Age): self.name = name self.age = Age DEF play_son (Self): Print ( "% S is playing the son of"% self.name) class Son (Father ): Money = 10 f1 = father ( "Cai Xu Kun", "30") Print (Son.money) s1 = Son ( "he", "18") # instantiated when the trigger Son inherits the parent class init, it requires two arguments Print (s1.name) Print (S1 .__ dict__ magic) Print (Son .__ dict__ magic)
Second, when to use inheritance?
When significantly different from, and is a component of a larger class of smaller class 1. When required between classes, in combination with better
There are many of the same functions between 2. When the class to extract these common features make the base class, inheritance is better
Derived:
A class that inherits the parent class, and have their own unique feature called derived
Inheritance also has two meanings:
1, inheriting the base class methods, and make their own changes or extensions (code reuse)
# This succession of little significance, even harmful, because it enables the base class subclasses occurs more often as possible independent of the strong coupling between the code block
2, a subclass declared compatible with certain base class that defines an interface class, subclass inherits the interface, and implementation of the method defined in the interface
Third, the interface inheritance []
Concepts: define a base class, which when base class to define its own way into the interface function, using the decorator manner,
As long as a subclass inherits to it, the base class must implement
Interface: is the function or method
Interface in the method do not realize, but in order to standardize the subclass.
By introducing abc module, subclass force must be consistent with the base class, and if not, an error!
import abc class All_file(metaclass = abc.ABCMeta): @abc.abstractclassmethod def read(self): pass @abc.abstractclassmethod def write(self): pass class Disk(All_file): def read(self): print('disk read') def write(self): print('disk write') class Cdrom(All_file): def read(self): print('cdrom read') #此处少定义一个write函数,所以会报错 c1 = Cdrom()
Disk and methods consistent with the base class in the class, the normal call
import abc class All_file(metaclass = abc.ABCMeta): @abc.abstractclassmethod def read(self): pass @abc.abstractclassmethod def write(self): pass class Disk(All_file): def read(self): print('disk read') def write(self): print('disk write') class Cdrom(All_file): def read(self): print('cdrom read') d1 = Disk() d1.read()
Fourth, the order of succession
Order of succession is divided into two types: 1. 2. breadth-first depth-first
No base class inheritance is called Classic
Base class inherits object called the new class
In python 3, the all new class, the new class of breadth-first
class A: #基类 def test(sslf): print('A') class B(A): def test(self): print('B') class C(A): def test(self): print('C') class D(C): def test(self): print('D') class E(C): def test(self): print('C') class F(D, E): def test(self): print('F') f1 = F() f1.test()
When you call test method, starting with itself began to look for, find it, look for inherited parent class, the new class of breadth-first
F--->D--->B--->E--->C---->A
Built-in way to see the order of succession
python in the end is how to implement inheritance, for each class you define, a python calculates the analytical method (MRO) list,
The MRO list is a list of simple linear sequence of all the base class
In order to achieve inheritance, python will start looking from left to right on the MRO base class list until it finds the first match of this property class.
This list is constructed MRO is achieved by a linear algorithm C3. We do not go into the mathematical principle of this algorithm, it actually
MRO is to merge a list of all the parent class and follow the following three rules:
1. subclasses are checked before the parent
2. A plurality of parent classes based on their order in the list is checked
3. If there are two legal option for the next class, selecting a first parent
print(F.__mro__)
In python2, there are sub-categories of classic, classic class inheritance depth-first
即:F--->D---->B--->A---->E----->C