First, inheritance
Inheritance is a way to create a new class, in Python, the new class can inherit from one or more parent classes, the original class is called a base class or a superclass, and the new class is called a derived class or subclass.
The inheritance of classes in Python is divided into: single inheritance and multiple inheritance, if it is multiple inheritance, the inheritance order has depth and breadth of 2 kinds
Example:
Class ParentClass1: #定义父类 passclass ParentClass2: #定义父类 passclass SubClass1 (PARENTCLASS1): #单继承, The base class is ParentClass1, and the derived class is subclass Passclass SubClass2 (parentclass1,parentclass2): #python支持多继承, separating multiple inherited classes with commas Pass
View Inheritance:
Example:
>>> subclass1.__bases__ (<class ' __main__. ParentClass1 ',,) >>> subclass2.__bases__ (<class ' __main__. ParentClass1 ';, <class ' __main__. ParentClass2 ' >)
Tip: If you do not specify a base class, the Python class inherits the object class by default, object is the base class for all Python classes, and it provides implementations of some common methods, such as __str__.
Example:
>>> parentclass1.__bases__ (<class ' object ';,) >>> parentclass2.__bases__ (<class ' object ') ,)
Inheritance has two meanings at the same time
Meaning one. Inherit the method of the base class and make your own changes or extensions (code reuse)
Meaning two. Declares that a subclass is compatible with a base class, defines an interface class, inherits the interface class, and implements the methods defined in the interface
The greatest benefit of inheritance is the reduction of code reuse
Example:
Class Hero: #父类 def __init__ (self,nickname,aggressivity=58,life_value=455,): self.nickname=nickname Self.aggressivity=aggressivity self.life_value=life_value def attack (Self,enemy): enemy.life_ Value-=self.aggressivity Print (Enemy.life_value) class Garen (Hero): #子类继承父类hero camp= ' Demacia ' # This hero all inherits the content of the parent class, only the camp is different class Riven (Hero): #子类继承父类hero #这个英雄人物全部继承父类的内容, only the camp is not the same camp= ' Noxus ' G1=garen (" Invincible ") # print (G1) r1=riven (" Meng Meng ") g1.attack (R1) #攻击萌萌
The execution results are:
397
The code above inherits the class of hero heroes, so there are common attributes within the class, which greatly reduces the reusability of the code.
Second, derivation
However, inheritance sometimes does not satisfy the requirements in the scenario, and sometimes subclasses need to add their own new attributes or redefine them here (without affecting the parent class), and it is important to note that once you have redefined your own properties and have the same name as the parent class, you will have to invoke the new attributes when you call them. So this concept is equivalent to "derivation"
Derivation: first inherits the parent class and then defines its own constructor method
Use the example above in this example:
Class Hero: #父类 def __init__ (self,nickname,aggressivity=58,life_value=455,): Self.nickname=nickname SELF.A Ggressivity=aggressivity Self.life_value=life_value def attack (Self,enemy): Enemy.life_value-=self.aggres Sivity print (Enemy.life_value, ' Hero attack ') class Garen (Hero): #子类继承父类hero camp= ' Demacia ' def __init__ (Self,ni ckname,aggressivity=58,life_value=455,money=100): hero.__init__ (self,nickname,aggressivity=58,life_value=455,) # Calling the parent class's function __init__ (self,nickname,aggressivity=58,life_value=455,) self.money=money# derives its new features out of Def attack (Self,en Emy,tiao): Hero.attack (Self,enemy) #调用父类的函数attack (Self,enemy) self.tiao=tiao# derive their own new features out of print (' from Garen attack ') def file: #新加技能 print ("%s is filing"%self.nickname) class Riven (Hero): #子类继承父类hero #这个英雄人物 All inherit the contents of the parent class, only the camp is different camp= ' Noxus ' G1=garen ("Invincible") G1.file () #打印新加的技能print (G1.money) #打印新加的money值 # Print (G1) r1=riven ("Moe" ) G1.attack (R1, "enemy") #攻击萌萌g1. AttacK (R1, "enemy") #攻击萌萌
The result of the execution is:
Invincible is filing100397 Hero attackfrom garen attack339 Hero attackfrom Garen attack
About the order of inheritance:
Inheritance differs from one version of Python to another .
Python3: New Class (breadth first)
Pyhon2: New class (breadth first) and classic class (depth first)
Example Description:
The inheritance order of the new class is as follows: (Python2 and Python3 are the same)
The code is:
Class A: def __init__ (self): print (' A method of construction ') class B (a): def __init__ (self): print (' construction method of ' B ') passclass C (A): def __init__ (self): print (construction method of ' C ') passclass D (B): def __init__ (self): Print (construction method of ' D ') passclass E (C): def __init__ (self): print (' construction method of E ') passclass F (d,e): def __init__ (self): print (' construction method of F ') passf1=f () #实例化触发运行
The order of execution is:
f--"d--" b--"e--" c--"a--" Object
The implementation of the Classic class is: (only available in Python2)
The inheritance order of classic classes such as: (depth first)
In code is the implementation is:
#python2中经典类的继承, follow when searching for attributes: Depth First class A: # def test (self): # print (' from A ') passclass B (a): # def Test (self): # print ("from B") passclass C (A): # def test (self): # print (' from C ') Passclass D (B): # def test (self): # print (' from D ') passclass E (C): # def test (self): # print (' from E ') passclass F (d,e): # def test (self): # print (' from F ') passf1=f () F1.test () # F->d->b->a->e->c
The order of execution is:
F->d->b->a->e->c
See how Python implements Inheritance (using the class name. Mor () view)
How python actually implements inheritance, for each class you define, Python calculates a list of method parsing orders (MRO), which is a simple linear sequential list of all base classes, such as
>>> F.mro () #等同于F. __mro__[<class ' __main__. F ';, <class ' __main__. D ';, <class ' __main__. B ';, <class ' __main__. E ';, <class ' __main__. C ';, <class ' __main__. A ';, <class ' object ';]
Summarize:
To implement inheritance, Python finds the base class from left to right on the MRO list until the first class that matches the property is found.
The construction of this MRO list is implemented by a C3 linearization algorithm. We're not going to delve into the math of this algorithm, it's actually merging all of the parent's MRO lists and following three guidelines:
1. Subclasses are checked before the parent class
2. Multiple parent classes are checked according to their order in the list
3. If there are two valid choices for the next class, select the first parent class
Python's object-oriented inheritance and derivation