This article mainly introduces detailed information about the inheritance instances of the python class. For more information about the inheritance instances of the python class, see the following article, for more information, see
Python class inheritance
For many articles that explain the inheritance of python classes, most of them refer to some concepts such as oop and polymorphism. I think this may not help developers who have a certain degree of foundation, it is better to directly use the code written in various situations to demonstrate the effect of code running on a specific code scenario. This may help developers more. Put the code directly without talking nonsense.
Classic classes and new classes are not distinguished here. The analysis below applies to both new classes and classic classes.
For the init function in the class, it is only a function called for initialization (ps: Initialization and instance creation are not a process, and instance creation is completed through a create function ), if the declared init function is not displayed in the subclass, the subclass will call the init function of the parent class, but will no longer call the init function of the parent class, if the init function is declared, the initialization function of the parent class will not be called during subclass initialization, but the init function declared in the subclass will be called, at the same time, no attributes declared in the parent class init function are available in the subclass instance,
Example:
class animal(): name="hh" sex="man" def init(self): self.height=10 self.weight=50 def deception(self): print "ansible.height:"+self.height+" animal.weight:"+self.weight def run(self): print "animal is running...."class dog(animal): def init(self): passif name=="main": dg=dog() print dg.dict
The running result is
{}
When the dog class is changed to the following (ps: the declared init method is not displayed at this time ):
class dog(animal): def run(self): print "dog is running..."
In this case, because the init method of the parent class animal is called directly, the result is as follows:
{'weight': 50, 'height': 10}
What happens if a class inherits the dog class? (The python interpreter first searches for the init method in the subclass. If no init method is found, find it from the parent class until it is found and run the command, the init method of the parent class is no longer running. the attributes created in the init method that are not running do not exist. for example, in the preceding example, the weight and height attributes are not found in the example obtained during the first running)
class animal(): name="hh" sex="" def init(self): self.height=10 self.weight=50 def deception(self): print "ansible.height:"+self.height+" animal.weight:"+self.weight def run(self): print "animal is running...."class dog(animal): def init(self): pass def run(self): print "dog is running..."class jinmao(dog): # def init(self): # self.ji="jinmao" passif name=="main": dg=jinmao() print dg.dict
The result is the same
{}
What happens to the multi-inheritance of classes? (In multi-inheritance, the init function in the parent class declared by the first parameter in the inherited parameter of the class will be run. if the parent class does not contain the init function, continue to find the parent class of the parent class, and so on... If the header still does not exist, replace it with the parent class declared by the second parameter. The process is like the first parameter, until the init function is found. if none is found, it is not initialized ), the code can be tested by yourself based on the previous example.
The usage and features of super that calls methods in the parent class in subclass.
Summary:
The above is the details of the class inheritance code (python). For more information, see other related articles in the first PHP community!